X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Fwrappers%2Fsdl2%2Fsdl2allegro.inc;h=fedd18bed2f73dfdd54b1655db1ad8bc9620d796;hb=ccbaf6953ba60385f0fd86ec5921c81ecd6fe08b;hp=a1f73934c7c901be5ae95760a0b7ccddcc8f4bb1;hpb=dea4e2426106ed2b46c3799c7b34a358a4c303aa;p=d2df-sdl.git diff --git a/src/wrappers/sdl2/sdl2allegro.inc b/src/wrappers/sdl2/sdl2allegro.inc index a1f7393..fedd18b 100644 --- a/src/wrappers/sdl2/sdl2allegro.inc +++ b/src/wrappers/sdl2/sdl2allegro.inc @@ -296,6 +296,13 @@ interface var sdl2allegro_screen: PBITMAP; sdl2allegro_bpp: Integer; + globalTransTable: COLOR_MAP_T; + redTransTable: COLOR_MAP_T; + greenTransTable: COLOR_MAP_T; + blueTransTable: COLOR_MAP_T; + darkTransTable: COLOR_MAP_T; + lightTransTable: COLOR_MAP_T; + /// FUNCTIONS /// @@ -365,20 +372,26 @@ implementation uses {$IFDEF GO32V2} + {$IFNDEF USE_ENETWRAP} + Watt32, + {$ENDIF} go32, {$ENDIF} - e_Log, g_options, SysUtils, ctypes; + e_Log, g_options, SysUtils, Math, Classes, ctypes; const maxKeyBuffer = 64; var + deskw, deskh, deskbpp: Integer; keyring: array [0..maxKeyBuffer - 1] of Integer; keybeg, keyend: Integer; inputChar: Char; inputText: Boolean; + useVsync: Boolean; ticks: UInt32; quit: Boolean; + custompal: PALETTE; s2lc: array [0..KEY_MAX] of char = ( #00, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', @@ -531,6 +544,48 @@ implementation SDL_SCANCODE_UNKNOWN (* KEY_MAX *) ); + procedure LoadCustomPalette (const name: String); + var f: TFileStream; i: Integer; + begin + try + f := TFileStream.Create(name, fmOpenRead); + e_LogWriteLn('Load custom palette ' + name); + for i := 0 to 255 do + begin + custompal[i].r := f.ReadByte(); + custompal[i].g := f.ReadByte(); + custompal[i].b := f.ReadByte(); + custompal[i].filler := $FF; + end; + f.Destroy + except + e_LogWriteLn('Fallback to default palette'); + custompal := default_palette; +(* + for i := 0 to 255 do + begin + custompal[i].r := i div 4; + custompal[i].g := i div 4; + custompal[i].b := i div 4; + custompal[i].filler := $FF; + end; +*) + end + end; + + procedure UpdatePalette; + begin + set_palette(custompal); + select_palette(custompal); + create_trans_table(@globalTransTable, custompal, 255, 255, 255, nil); + create_trans_table(@redTransTable, custompal, 0, 255, 255, nil); + create_trans_table(@greenTransTable, custompal, 255, 0, 255, nil); + create_trans_table(@blueTransTable, custompal, 255, 255, 0, nil); + create_trans_table(@darkTransTable, custompal, 191, 191, 191, nil); + create_trans_table(@lightTransTable, custompal, 64, 64, 64, nil); + color_map := @globalTransTable; + end; + function IsEmptyKeyboard: Boolean; begin result := keybeg = keyend @@ -599,12 +654,12 @@ implementation function SDL_RWFromFile(const _file: PAnsiChar; const mode: PAnsiChar): PSDL_RWops; begin - result := nil + result := nil (* stub, used for sdl2stub_mixer *) end; function SDL_RWFromConstMem(const mem: Pointer; size: SInt32): PSDL_RWops; begin - result := nil + result := nil (* stub, used for sdl2stub_mixer *) end; (********** KEYBOARD **********) @@ -825,7 +880,10 @@ implementation procedure AllegroTimerCallback; cdecl; begin - inc(ticks) + inc(ticks); + {$IF DEFINED(GO32V2) AND NOT DEFINED(USE_ENETWRAP)} + userTimerTick(1); + {$ENDIF} end; procedure AllegroTimerCallbackEND; begin end; @@ -846,34 +904,84 @@ implementation function SDL_GetTicks: UInt32; begin - result := ticks; + result := ticks end; (********** DISPLAY MODES **********) + function GetPixelFormat (bpp: Integer): UInt32; + begin + case bpp of + 8: result := SDL_PIXELFORMAT_INDEX8; + 15: result := SDL_PIXELFORMAT_RGB555; + 16: result := SDL_PIXELFORMAT_RGB565; + 24: result := SDL_PIXELFORMAT_RGB888; + 32: result := SDL_PIXELFORMAT_RGBA8888; + else result := SDL_PIXELFORMAT_UNKNOWN + end + end; + function SDL_GetDesktopDisplayMode(displayIndex: SInt32; mode: PSDL_DisplayMode): SInt32; begin - e_LogWritefln('SDL_GetDesktopDisplayMode %s', [displayIndex]); - result := -1; - mode.format := SDL_PIXELFORMAT_UNKNOWN; (* !!! *) + ASSERT(mode <> nil); + mode.format := GetPixelFormat(deskbpp); + mode.w := deskw; + mode.h := deskh; mode.refresh_rate := 0; mode.driverdata := nil; - if get_desktop_resolution(@mode.w, @mode.h) = 0 then - result := 0 + result := 0 end; function SDL_GetClosestDisplayMode(displayIndex: SInt32; const mode: PSDL_DisplayMode; closest: PSDL_DisplayMode): PSDL_DisplayMode; + var m: PGFX_MODE_LIST; i, bpp: Integer; begin - e_LogWritefln('SDL_GetClosestDisplayMode %s', [displayIndex]); + ASSERT(mode <> nil); + ASSERT(closest <> nil); + result := nil; + bpp := SDL_BITSPERPIXEL(mode.format); + if bpp = 0 then bpp := deskbpp; + + m := nil; + if (gfx_driver <> nil) and (@gfx_driver.fetch_mode_list <> nil) then + m := gfx_driver.fetch_mode_list; + + if m <> nil then + begin + i := 0; + while (i < m.num_modes) and (m.mode[i].bpp <> bpp) do inc(i); + if i < m.num_modes then + begin + closest.format := mode.format; + closest.refresh_rate := 0; + closest.driverdata := nil; + closest.w := m.mode[i].width; + closest.h := m.mode[i].height; + result := closest + end; + while i < m.num_modes do + begin + if (m.mode[i].bpp = bpp) and (closest.w >= mode.w) and (m.mode[i].width <= closest.w) and (closest.h >= mode.h) and (m.mode[i].height <= closest.h) then + begin + closest.w := m.mode[i].width; + closest.h := m.mode[i].height; + result := closest + end; + inc(i) + end; + destroy_gfx_mode_list(m) + end end; function SDL_GetNumDisplayModes(displayIndex: SInt32): SInt32; var m: PGFX_MODE_LIST; begin - e_LogWritefln('SDL_GetNumDisplayModes %s', [displayIndex]); result := -1; - m := get_gfx_mode_list(GFX_AUTODETECT); + + m := nil; + if (gfx_driver <> nil) and (@gfx_driver.fetch_mode_list <> nil) then + m := gfx_driver.fetch_mode_list; + if m <> nil then begin result := m.num_modes; @@ -884,24 +992,30 @@ implementation function SDL_GetDisplayMode(displayIndex: SInt32; modeIndex: SInt32; mode: PSDL_DisplayMode): SInt32; var m: PGFX_MODE_LIST; begin - e_LogWritefln('SDL_GetDisplayMode %s %s', [displayIndex, modeIndex]); result := -1; - m := get_gfx_mode_list(GFX_AUTODETECT); - if (m <> nil) and (modeIndex >= 0) and (modeIndex < m.num_modes) then + + m := nil; + if (gfx_driver <> nil) and (@gfx_driver.fetch_mode_list <> nil) then + m := gfx_driver.fetch_mode_list; + + if m <> nil then begin - mode.format := SDL_PIXELFORMAT_UNKNOWN; (* FIX IT *) - mode.w := m.mode[modeIndex].width; - mode.h := m.mode[modeIndex].height; - mode.refresh_rate := 0; - mode.driverdata := nil; - destroy_gfx_mode_list(m); - result := 0 + if (modeIndex >= 0) and (modeIndex < m.num_modes) then + begin + mode.format := GetPixelFormat(m.mode[modeIndex].bpp); + mode.w := m.mode[modeIndex].width; + mode.h := m.mode[modeIndex].height; + mode.refresh_rate := 0; + mode.driverdata := nil; + result := 0 + end; + destroy_gfx_mode_list(m) end end; function SDL_GetDisplayDPI(displayIndex: SInt32; ddpi, hdpi, vdpi: PFloat): SInt32; begin - result := -1 + result := -1 (* stub *) end; (*********** WINDOW MANAGEMENT **********) @@ -909,7 +1023,6 @@ implementation function SDL_CreateWindow(const title: PAnsiChar; x: SInt32; y: SInt32; w: SInt32; h: SInt32; flags: UInt32): PSDL_Window; var window: PSDL_Window; mode: Integer; begin - e_LogWritefln('SDL_CreateWindow %s %s %s %s %s %u', [title, x, y, w, h, flags]); result := nil; {$IF DEFINED(GO32V2)} @@ -921,7 +1034,9 @@ implementation mode := GFX_AUTODETECT_WINDOWED; {$ENDIF} - sdl2allegro_bpp := gBPP; + if sdl2allegro_bpp = 0 then sdl2allegro_bpp := gBPP; + if sdl2allegro_bpp = 0 then sdl2allegro_bpp := deskbpp; + set_color_depth(sdl2allegro_bpp); if set_gfx_mode(mode, w, h, 0, 0) = 0 then begin @@ -936,6 +1051,7 @@ implementation window.w := w; window.h := h; window.mode := mode; + UpdatePalette; result := window end end; @@ -943,9 +1059,8 @@ implementation function SDL_SetWindowFullscreen(window: PSDL_Window; flags: UInt32): SInt32; var mode: Integer; begin - e_LogWritefln('SDL_SetWindowFullscreen %u', [flags]); + ASSERT(window <> nil); result := -1; - if window = nil then exit; {$IF DEFINED(GO32V2)} mode := GFX_AUTODETECT; @@ -965,7 +1080,7 @@ implementation if sdl2allegro_screen = nil then sdl2allegro_screen := create_bitmap(window.w, window.h); ASSERT(sdl2allegro_screen <> nil); - set_palette(desktop_palette); + UpdatePalette; window.mode := mode; result := 0 end @@ -973,9 +1088,7 @@ implementation procedure SDL_SetWindowSize(window: PSDL_Window; w: SInt32; h: SInt32); begin - e_LogWritefln('SDL_SetWindowSize %s %s', [w, h]); - if window = nil then exit; - + ASSERT(window <> nil); set_color_depth(sdl2allegro_bpp); if set_gfx_mode(window.mode, w, h, 0, 0) = 0 then begin @@ -985,15 +1098,18 @@ implementation if sdl2allegro_screen = nil then sdl2allegro_screen := create_bitmap(w, h); ASSERT(sdl2allegro_screen <> nil); - set_palette(desktop_palette); + UpdatePalette; window.w := w; - window.h := h; + window.h := h + end + else + begin + ASSERT(FALSE, 'Unable to set window size') end end; procedure SDL_DestroyWindow(window: PSDL_Window); begin - e_LogWriteln('SDL_DestroyWindow'); if window = nil then exit; if sdl2allegro_screen <> nil then destroy_bitmap(sdl2allegro_screen); @@ -1003,35 +1119,27 @@ implementation procedure SDL_GetWindowSize(window: PSDL_Window; w: PInt; h: PInt); begin - e_LogWriteln('SDL_GetWindowSize'); - if window = nil then exit; - if w <> nil then - w^ := window.w; - if h <> nil then - h^ := window.h; + ASSERT(window <> nil); + if w <> nil then w^ := window.w; + if h <> nil then h^ := window.h; end; procedure SDL_RestoreWindow(window: PSDL_Window); begin - e_LogWriteln('SDL_RestoreWindow'); - if window = nil then exit; + ASSERT(window <> nil); (* stub *) end; function SDL_SetWindowGammaRamp(window: PSDL_Window; const red: PUInt16; const green: PUInt16; const blue: PUInt16): SInt32; begin - e_LogWriteln('SDL_SetWindowGammaRamp'); - result := -1; - if window = nil then exit; - result := 0 + ASSERT(window <> nil); + result := 0 (* stub *) end; function SDL_GetWindowGammaRamp(window: PSDL_Window; red: PUInt16; green: PUInt16; blue: PUInt16): SInt32; begin - e_LogWriteln('SDL_GetWindowGammaRamp'); - result := -1; - if window = nil then exit; - result := 0 + ASSERT(window <> nil); + result := 0 (* stub *) end; (********** OPENGL MANAGEMENT **********) @@ -1051,34 +1159,45 @@ implementation function SDL_GL_CreateContext(window: PSDL_Window): TSDL_GLContext; begin - e_LogWriteln('SDL_GL_CreateContext'); - result := window + result := window (* stub *) end; function SDL_GL_MakeCurrent(window: PSDL_Window; context: TSDL_GLContext): SInt32; begin - e_LogWriteln('SDL_GL_MakeCurrent'); - result := 0 + ASSERT(window <> nil); + if context <> nil then + ASSERT(window = context); + result := 0 (* stub *) end; procedure SDL_GL_DeleteContext(context: TSDL_GLContext); begin - e_LogWriteln('SDL_GL_DeleteContext'); + (* stub *) end; function SDL_GL_SetSwapInterval(interval: SInt32): SInt32; begin - e_LogWritefln('SDL_GL_SetSwapInterval %s', [interval]); - result := 0 + result := 0; + if interval = 0 then + useVsync := False + else + useVsync := True end; procedure SDL_GL_SwapWindow(window: PSDL_Window); begin + ASSERT(window <> nil); ASSERT(sdl2allegro_screen <> nil); acquire_screen; - blit(sdl2allegro_screen, screen, 0, 0, 0, 0, sdl2allegro_screen.w, sdl2allegro_screen.h); + blit(sdl2allegro_screen, screen, 0, 0, 0, 0, min(sdl2allegro_screen.w, screen.w), min(sdl2allegro_screen.h, screen.h)); show_video_bitmap(screen); release_screen; + if useVsync then + vsync; + + {$IF DEFINED(GO32V2) AND NOT DEFINED(USE_ENETWRAP)} + tcp_tick(nil); + {$ENDIF} end; (********** EVENTS **********) @@ -1088,7 +1207,9 @@ implementation result := 1; case event.type_ of SDL_QUITEV: quit := True; - else ASSERT(FALSE); result := 0 + else + ASSERT(FALSE); + result := 0 end end; @@ -1116,13 +1237,17 @@ implementation inputChar := #0; result := 1; Exit - end; + end + else + inputChar := #0; poll_keyboard; if not IsEmptyKeyboard then begin alscan := NextScancode; pressed := alscan and $80 = 0; + + inputChar := #0; if pressed then begin shift := key_shifts and KB_SHIFT_FLAG <> 0; @@ -1134,34 +1259,36 @@ implementation event.type_ := SDL_KEYDOWN end else - begin - inputChar := #0; - event.type_ := SDL_KEYUP - end; + event.type_ := SDL_KEYUP; + event.key.timestamp := ticks; event.key.windowID := 0; + (**** df not use it? if pressed then event.key.state := SDL_PRESSED else event.key.state := SDL_RELEASED; ****) + event.key._repeat := 0; event.key.keysym.scancode := s2sa[alscan and $7F]; event.key.keysym.sym := 0; (* df not use it? *) event.key.keysym._mod := 0; (* df not use it? *) result := 1; Exit - end + end; + + {$IF DEFINED(GO32V2) AND NOT DEFINED(USE_ENETWRAP)} + tcp_tick(nil); + {$ENDIF} end; (********** MOUSE **********) function SDL_ShowCursor(toggle: SInt32): SInt32; begin - e_LogWritefln('SDL_ShowCursor %s', [toggle]); - (* TODO *) - result := 0 + result := 0 (* stub *) end; (********** SDL **********) @@ -1172,16 +1299,15 @@ implementation end; procedure AllegroQuitCallbackEND; begin end; - function SDL_SetHint( const name: PChar; const value: PChar) : boolean; + function SDL_SetHint(const name: PChar; const value: PChar): boolean; begin e_LogWritefln('SDL_SetHint %s %s', [name, value]); - (* TODO *) - result := false + result := false (* stub *) end; function SDL_GetError: PAnsiChar; begin - result := allegro_error; + result := allegro_error end; {$IFDEF GO32V2} @@ -1224,6 +1350,14 @@ implementation set_keyboard_rate(0, 0); install_int_ex(AllegroTimerCallback, MSEC_TO_TIMER(1)); set_close_button_callback(AllegroQuitCallback); + deskbpp := desktop_color_depth; + if deskbpp = 0 then deskbpp := 8; + if get_desktop_resolution(@deskw, @deskh) <> 0 then + begin + deskw := 640; + deskh := 480 + end; + LoadCustomPalette('PLAYPAL.LMP'); result := 0 end end;