diff --git a/CMakeLists.txt b/CMakeLists.txt index cf13adf..8ce1dbc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,7 +43,6 @@ set(LOADTRK_SOURCES src/asm/parse.c src/asm/pc.c src/asm/vec.c - src/bme/bme_gfx.cpp src/bme/bme_snd.cpp src/bme/bme_win.cpp src/bme/bme_io.cpp diff --git a/src/bme/bme_gfx.cpp b/src/bme/bme_gfx.cpp deleted file mode 100644 index 189cf0e..0000000 --- a/src/bme/bme_gfx.cpp +++ /dev/null @@ -1,358 +0,0 @@ -// -// BME (Blasphemous Multimedia Engine) graphics main module -// - -#include "bme_gfx.h" - -#include "bme_main.h" -#include "bme_win.h" -#include "bme_io.h" - -#include - -#include - -#include -#include -#include - -// Colodore palette -unsigned char gfx_palette[MAX_COLORS * 3] = -{ - // Black - 0x00, 0x00, 0x00, - // White - 0xFF, 0xFF, 0xFF, - // Red - 0x96, 0x28, 0x2e, - // Cyan - 0x5b, 0xd6, 0xce, - // Purple - 0x9f, 0x2d, 0xad, - // Green - 0x41, 0xb9, 0x36, - // Blue - 0x27, 0x24, 0xc4, - // Yellow - 0xef, 0xf3, 0x47, - // Orange - 0x9f, 0x48, 0x15, - // Brown - 0x5e, 0x35, 0x00, - // Light Red - 0xda, 0x5f, 0x66, - // Dark Gray - 0x47, 0x47, 0x47, - // Medium Gray - 0x78, 0x78, 0x78, - // Light Green - 0x91, 0xff, 0x84, - // Light Blue - 0x68, 0x64, 0xff, - // Light Gray - 0xae, 0xae, 0xae -}; - -void gfx_setclipregion(unsigned left, unsigned top, unsigned right, unsigned bottom); - -bool gfx_initted = false; -bool gfx_redraw = false; -int gfx_scanlinemode = 0; -unsigned gfx_virtualxsize; -unsigned gfx_virtualysize; -unsigned gfx_windowxsize; -unsigned gfx_windowysize; -int spr_xsize = 0; -int spr_ysize = 0; -SDL_Surface *gfx_screen = nullptr; -SDL_Renderer *gfx_renderer = nullptr; - -// Static variables - -static bool gfx_initexec = false; -static unsigned gfx_last_xsize; -static unsigned gfx_last_ysize; -static unsigned gfx_last_framerate; -static unsigned gfx_last_flags; -static int gfx_cliptop; -static int gfx_clipbottom; -static int gfx_clipleft; -static int gfx_clipright; - -static SDL_Surface *gfx_cursor = nullptr; - -static SDL_Color gfx_sdlpalette[MAX_COLORS]; -static bool gfx_locked = false; -static SDL_Texture *sdlTexture = nullptr; - -bool gfx_init(unsigned xsize, unsigned ysize, unsigned framerate, unsigned flags) -{ - // Prevent re-entry (by window procedure) - if (gfx_initexec) return true; - gfx_initexec = true; - - gfx_last_xsize = xsize; - gfx_last_ysize = ysize; - gfx_last_framerate = framerate; - gfx_last_flags = flags & ~(GFX_FULLSCREEN | GFX_WINDOW); - - // Store the options contained in the flags - - gfx_scanlinemode = flags & (GFX_SCANLINES | GFX_DOUBLESIZE); - - SDL_SetWindowFullscreen(win_window, win_fullscreen); - - // Calculate virtual window size - - gfx_virtualxsize = xsize; - gfx_virtualxsize /= 16; - gfx_virtualxsize *= 16; - gfx_virtualysize = ysize; - - if ((!gfx_virtualxsize) || (!gfx_virtualysize)) - { - gfx_initexec = false; - gfx_uninit(); - bme_error = BME_ILLEGAL_CONFIG; - return false; - } - - // Calculate actual window size (for scanline mode & doublesize mode - // this is double the virtual) - - gfx_windowxsize = gfx_virtualxsize; - gfx_windowysize = gfx_virtualysize; - if (gfx_scanlinemode) - { - gfx_windowxsize <<= 1; - gfx_windowysize <<= 1; - } - - gfx_setclipregion(0, 0, gfx_virtualxsize, gfx_virtualysize); - - gfx_renderer = SDL_CreateRenderer(win_window, nullptr); - gfx_screen = SDL_CreateSurface(xsize, ysize, SDL_PIXELFORMAT_INDEX8); - sdlTexture = SDL_CreateTexture(gfx_renderer, - SDL_PIXELFORMAT_RGBA32, - SDL_TEXTUREACCESS_STREAMING, - xsize, ysize); - - gfx_initexec = false; - if (gfx_screen) - { - gfx_initted = true; - gfx_redraw = true; - gfx_setpalette(); - win_setmousemode(win_mousemode); - return true; - } - else return false; -} - -int gfx_reinit() -{ - gfx_uninit(); - return gfx_init(gfx_last_xsize, gfx_last_ysize, gfx_last_framerate, - gfx_last_flags); -} - -void gfx_uninit() -{ - SDL_DestroyTexture(sdlTexture); - SDL_DestroySurface(gfx_screen); - SDL_DestroyRenderer(gfx_renderer); - gfx_initted = false; - return; -} - -bool gfx_lock() -{ - if (gfx_locked) return true; - if (!gfx_initted) return false; - if (SDL_LockSurface(gfx_screen)) - { - gfx_locked = true; - return true; - } - else return false; -} - -void gfx_unlock() -{ - if (gfx_locked) - { - SDL_UnlockSurface(gfx_screen); - gfx_locked = false; - } -} - -void gfx_flip() -{ - SDL_Surface* surf = SDL_ConvertSurface(gfx_screen, SDL_PIXELFORMAT_RGBA32); - SDL_UpdateTexture(sdlTexture, nullptr, surf->pixels, surf->pitch); - SDL_DestroySurface(surf); - SDL_RenderClear(gfx_renderer); - SDL_RenderTexture(gfx_renderer, sdlTexture, nullptr, nullptr); - SDL_RenderPresent(gfx_renderer); - gfx_redraw = false; -} - -void gfx_calcpalette() -{ - unsigned char *sptr = gfx_palette; - for (int c = 0; c < MAX_COLORS; c++) - { - gfx_sdlpalette[c].r = *sptr; - sptr++; - - gfx_sdlpalette[c].g = *sptr; - sptr++; - - gfx_sdlpalette[c].b = *sptr; - sptr++; - - gfx_sdlpalette[c].a = 255; - } -} - -void gfx_setpalette() -{ - if (!gfx_initted) return; - - gfx_calcpalette(); - SDL_Palette *palette = SDL_CreateSurfacePalette(gfx_screen); - SDL_SetPaletteColors(palette, &gfx_sdlpalette[0], 0, MAX_COLORS); -} - -void gfx_setclipregion(unsigned left, unsigned top, unsigned right, unsigned bottom) -{ - if (left >= right) return; - if (top >= bottom) return; - if (left >= gfx_virtualxsize) return; - if (top >= gfx_virtualysize) return; - if (right > gfx_virtualxsize) return; - if (bottom > gfx_virtualysize) return; - - gfx_clipleft = left; - gfx_clipright = right; - gfx_cliptop = top; - gfx_clipbottom = bottom; -} - -bool gfx_loadcursor(const char *name) -{ - bme_error = BME_OPEN_ERROR; - - gfx_freecursor(); - - int handle = io_open(name); - if (handle == -1) return false; - - int size = io_lseek(handle, 0, SEEK_END); - io_lseek(handle, 0, SEEK_SET); - char *iconbuffer = new (std::nothrow) char[size]; - io_read(handle, iconbuffer, size); - io_close(handle); - - SDL_IOStream *rw = SDL_IOFromMem(iconbuffer, size); - gfx_cursor = SDL_LoadPNG_IO(rw, true); - if (!gfx_cursor) - return false; - - bme_error = BME_OK; - return true; -} - -void gfx_freecursor() -{ - if (gfx_cursor) - { - SDL_DestroySurface(gfx_cursor); - gfx_cursor = nullptr; - } -} - -void gfx_copyscreen8(Uint8 *destaddress, Uint8 *srcaddress, unsigned pitch) -{ - switch(gfx_scanlinemode) - { - default: - for (unsigned c = 0; c < gfx_virtualysize; c++) - { - std::memcpy(destaddress, srcaddress, gfx_virtualxsize); - destaddress += pitch; - srcaddress += gfx_virtualxsize; - } - break; - - case GFX_SCANLINES: - for (unsigned c = 0; c < gfx_virtualysize; c++) - { - int d = gfx_virtualxsize; - while (d--) - { - *destaddress = *srcaddress; - destaddress++; - *destaddress = *srcaddress; - destaddress++; - srcaddress++; - } - destaddress += pitch*2 - (gfx_virtualxsize << 1); - } - break; - - case GFX_DOUBLESIZE: - for (unsigned c = 0; c < gfx_virtualysize; c++) - { - int d = gfx_virtualxsize; - while (d--) - { - *destaddress = *srcaddress; - destaddress++; - *destaddress = *srcaddress; - destaddress++; - srcaddress++; - } - destaddress += pitch - (gfx_virtualxsize << 1); - srcaddress -= gfx_virtualxsize; - d = gfx_virtualxsize; - while (d--) - { - *destaddress = *srcaddress; - destaddress++; - *destaddress = *srcaddress; - destaddress++; - srcaddress++; - } - destaddress += pitch - (gfx_virtualxsize << 1); - } - break; - } -} - -void gfx_drawcursor(int x, int y) -{ - if (!gfx_initted) return; - //if (!gfx_locked) return; - - if (!gfx_cursor) - { - spr_xsize = 0; - spr_ysize = 0; - return; - } - - spr_xsize = gfx_cursor->w; - spr_ysize = gfx_cursor->h; - - if (x >= gfx_clipright) return; - if (y >= gfx_clipbottom) return; - if (x + spr_xsize <= gfx_clipleft) return; - if (y + spr_ysize <= gfx_cliptop) return; - - SDL_Rect dstrect; - dstrect.x = x; - dstrect.y = y; - if (!SDL_BlitSurface(gfx_cursor, NULL, gfx_screen, &dstrect)) - printf("Error: %s\n", SDL_GetError()); -} diff --git a/src/bme/bme_gfx.h b/src/bme/bme_gfx.h deleted file mode 100644 index d3edb98..0000000 --- a/src/bme/bme_gfx.h +++ /dev/null @@ -1,32 +0,0 @@ -// BME graphics module header file - -#ifndef BME_GFX_H -#define BME_GFX_H - -#include - -#define MAX_COLORS 16 // 8bit oldskool mode - -bool gfx_init(unsigned xsize, unsigned ysize, unsigned framerate, unsigned flags); -int gfx_reinit(); -void gfx_uninit(); -bool gfx_lock(); -void gfx_unlock(); -void gfx_flip(); - -void gfx_setpalette(); - -bool gfx_loadcursor( const char *name); -void gfx_drawcursor(int x, int y); -void gfx_freecursor(); - -extern bool gfx_initted; -extern bool gfx_redraw; -extern unsigned gfx_windowxsize; -extern unsigned gfx_windowysize; -extern unsigned gfx_virtualxsize; -extern unsigned gfx_virtualysize; -extern unsigned char gfx_palette[]; -extern SDL_Surface *gfx_screen; - -#endif diff --git a/src/bme/bme_io.cpp b/src/bme/bme_io.cpp index 36d5797..1621570 100644 --- a/src/bme/bme_io.cpp +++ b/src/bme/bme_io.cpp @@ -46,7 +46,7 @@ static void linkedseek(unsigned pos); static void linkedread(void *buffer, int length); static unsigned linkedreadle32(void); -int io_openlinkeddatafile(unsigned char *ptr) +bool io_openlinkeddatafile(unsigned char *ptr) { if (datafilehandle) std::fclose(datafilehandle); datafilehandle = nullptr; @@ -59,7 +59,7 @@ int io_openlinkeddatafile(unsigned char *ptr) if (std::memcmp(ident, idstring, 4)) { bme_error = BME_WRONG_FORMAT; - return BME_ERROR; + return false; } files = linkedreadle32(); @@ -67,7 +67,7 @@ int io_openlinkeddatafile(unsigned char *ptr) if (!fileheaders) { bme_error = BME_OUT_OF_MEMORY; - return BME_ERROR; + return false; } for (unsigned index = 0; index < files; index++) { @@ -79,7 +79,7 @@ int io_openlinkeddatafile(unsigned char *ptr) for (unsigned index = 0; index < MAX_HANDLES; index++) handle[index].open = false; io_datafileopen = true; bme_error = BME_OK; - return BME_OK; + return true; } void io_closelinkeddatafile() diff --git a/src/bme/bme_io.h b/src/bme/bme_io.h index 27dd47a..7811bc9 100644 --- a/src/bme/bme_io.h +++ b/src/bme/bme_io.h @@ -3,7 +3,7 @@ #ifndef BME_IO_H #define BME_IO_H -int io_openlinkeddatafile(unsigned char *ptr); +bool io_openlinkeddatafile(unsigned char *ptr); void io_closelinkeddatafile(); int io_open(const char *name); void io_close(int handle); diff --git a/src/bme/bme_main.h b/src/bme/bme_main.h index 392e28d..d40d2bd 100644 --- a/src/bme/bme_main.h +++ b/src/bme/bme_main.h @@ -5,43 +5,16 @@ #include -#define GFX_SCANLINES 1 -#define GFX_DOUBLESIZE 2 -#define GFX_USE1PAGE 0 -#define GFX_USE2PAGES 4 -#define GFX_USE3PAGES 8 -#define GFX_WAITVBLANK 16 -#define GFX_FULLSCREEN 32 -#define GFX_WINDOW 64 - -#define MOUSE_ALWAYS_VISIBLE 0 -#define MOUSE_FULLSCREEN_HIDDEN 1 -#define MOUSE_ALWAYS_HIDDEN 2 - #define MOUSEB_LEFT 1 #define MOUSEB_RIGHT 2 #define MOUSEB_MIDDLE 4 -#define LEFT 0 -#define MIDDLE 128 -#define RIGHT 255 - -#define B_OFF 0 -#define B_SOLID 1 -#define B_NOTSOLID 2 - #define MONO 0 #define STEREO 1 #define EIGHTBIT 0 #define SIXTEENBIT 2 #define FLOAT32BIT 4 -#define VM_OFF 0 -#define VM_ON 1 -#define VM_ONESHOT 0 -#define VM_LOOP 2 -#define VM_16BIT 4 - #define KEY_BACKSPACE SDL_SCANCODE_BACKSPACE #define KEY_CAPSLOCK SDL_SCANCODE_CAPSLOCK #define KEY_ENTER SDL_SCANCODE_RETURN @@ -175,24 +148,24 @@ typedef struct { - Sint8 *start; - Sint8 *repeat; - Sint8 *end; - unsigned char voicemode; + Sint8 *start; + Sint8 *repeat; + Sint8 *end; + unsigned char voicemode; } SAMPLE; typedef struct { - volatile Sint8 *pos; - Sint8 *repeat; - Sint8 *end; - SAMPLE *smp; - unsigned freq; - volatile unsigned fractpos; - int vol; - int mastervol; - unsigned panning; - volatile unsigned voicemode; + volatile Sint8 *pos; + Sint8 *repeat; + Sint8 *end; + SAMPLE *smp; + unsigned freq; + volatile unsigned fractpos; + int vol; + int mastervol; + unsigned panning; + volatile unsigned voicemode; } CHANNEL; typedef struct diff --git a/src/bme/bme_snd.cpp b/src/bme/bme_snd.cpp index 585f72f..1b617b9 100644 --- a/src/bme/bme_snd.cpp +++ b/src/bme/bme_snd.cpp @@ -23,6 +23,12 @@ #include #include +#define VM_OFF 0 +#define VM_ON 1 +#define VM_ONESHOT 0 +#define VM_LOOP 2 +#define VM_16BIT 4 + #ifdef USE_JACK typedef jack_default_audio_sample_t sample_t; #endif diff --git a/src/bme/bme_win.cpp b/src/bme/bme_win.cpp index 779c2f8..ddcdaa7 100644 --- a/src/bme/bme_win.cpp +++ b/src/bme/bme_win.cpp @@ -5,24 +5,66 @@ #include "bme_win.h" #include "bme_main.h" -#include "bme_gfx.h" #include "bme_io.h" #include +#include + #include #include #include +// Colodore palette +unsigned char gfx_palette[MAX_COLORS * 3] = +{ + // Black + 0x00, 0x00, 0x00, + // White + 0xFF, 0xFF, 0xFF, + // Red + 0x96, 0x28, 0x2e, + // Cyan + 0x5b, 0xd6, 0xce, + // Purple + 0x9f, 0x2d, 0xad, + // Green + 0x41, 0xb9, 0x36, + // Blue + 0x27, 0x24, 0xc4, + // Yellow + 0xef, 0xf3, 0x47, + // Orange + 0x9f, 0x48, 0x15, + // Brown + 0x5e, 0x35, 0x00, + // Light Red + 0xda, 0x5f, 0x66, + // Dark Gray + 0x47, 0x47, 0x47, + // Medium Gray + 0x78, 0x78, 0x78, + // Light Green + 0x91, 0xff, 0x84, + // Light Blue + 0x68, 0x64, 0xff, + // Light Gray + 0xae, 0xae, 0xae +}; + SDL_Window *win_window = nullptr; void win_checkmessages(); +void gfx_setclipregion(unsigned left, unsigned top, unsigned right, unsigned bottom); +bool gfx_reinit(); +void gfx_uninit(); + // Global variables int win_fullscreen = 0; // By default windowed bool win_windowinitted = false; -int win_quitted = 0; +bool win_quitted = false; unsigned char win_keytable[SDL_SCANCODE_COUNT] = {0}; unsigned char win_asciikey = 0; unsigned win_mousexpos = 0; @@ -30,10 +72,20 @@ unsigned win_mouseypos = 0; unsigned win_mousexrel = 0; unsigned win_mouseyrel = 0; unsigned win_mousebuttons = 0; -int win_mousemode = MOUSE_FULLSCREEN_HIDDEN; float win_mouseywheel = 0.f; unsigned char win_keystate[SDL_SCANCODE_COUNT] = {0}; +bool gfx_initted = false; +bool gfx_redraw = false; +unsigned gfx_virtualxsize; +unsigned gfx_virtualysize; +unsigned gfx_windowxsize; +unsigned gfx_windowysize; +int spr_xsize = 0; +int spr_ysize = 0; +SDL_Surface *gfx_screen = nullptr; +SDL_Renderer *gfx_renderer = nullptr; + int bme_error = BME_OK; // Static variables @@ -43,7 +95,21 @@ static Uint64 win_currenttime = 0; static int win_framecounter = 0; static int win_activateclick = 0; -int win_openwindow(unsigned xsize, unsigned ysize, const char *appname) +static bool gfx_initexec = false; +static unsigned gfx_last_xsize; +static unsigned gfx_last_ysize; +static int gfx_cliptop; +static int gfx_clipbottom; +static int gfx_clipleft; +static int gfx_clipright; + +static SDL_Surface *gfx_cursor = nullptr; + +static SDL_Color gfx_sdlpalette[MAX_COLORS]; +static bool gfx_locked = false; +static SDL_Texture *sdlTexture = nullptr; + +bool win_openwindow(unsigned xsize, unsigned ysize, const char *appname) { Uint32 flags = SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS | SDL_WINDOW_RESIZABLE; if (win_fullscreen) flags |= SDL_WINDOW_FULLSCREEN; @@ -52,7 +118,7 @@ int win_openwindow(unsigned xsize, unsigned ysize, const char *appname) { if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK)) { - return BME_ERROR; + return false; } std::atexit(SDL_Quit); win_windowinitted = true; @@ -63,13 +129,13 @@ int win_openwindow(unsigned xsize, unsigned ysize, const char *appname) win_window = SDL_CreateWindow(appname, xsize, ysize, flags); if (!win_window) { - return BME_ERROR; + return false; } SDL_StartTextInput(win_window); - return BME_OK; + return true; } -void win_closewindow(void) +void win_closewindow() { SDL_StopTextInput(win_window); SDL_DestroyWindow(win_window); @@ -169,7 +235,7 @@ void win_checkmessages() break; case SDL_EVENT_QUIT: - win_quitted = 1; + win_quitted = true; break; case SDL_EVENT_TEXT_INPUT: @@ -209,36 +275,204 @@ void win_checkmessages() } } -void win_setmousemode(int mode) +bool gfx_init(unsigned xsize, unsigned ysize) { - win_mousemode = mode; + // Prevent re-entry (by window procedure) + if (gfx_initexec) return true; + gfx_initexec = true; + + gfx_last_xsize = xsize; + gfx_last_ysize = ysize; + + SDL_SetWindowFullscreen(win_window, win_fullscreen); - switch(mode) + // Calculate virtual window size + + gfx_virtualxsize = xsize; + gfx_virtualxsize /= 16; + gfx_virtualxsize *= 16; + gfx_virtualysize = ysize; + + if ((!gfx_virtualxsize) || (!gfx_virtualysize)) { - case MOUSE_ALWAYS_VISIBLE: - SDL_ShowCursor(); - break; + gfx_initexec = false; + gfx_uninit(); + bme_error = BME_ILLEGAL_CONFIG; + return false; + } - case MOUSE_FULLSCREEN_HIDDEN: - if (win_fullscreen) - { - SDL_HideCursor(); - } - else - { - SDL_ShowCursor(); - } - break; + // Calculate actual window size (for scanline mode & doublesize mode + // this is double the virtual) + + gfx_windowxsize = gfx_virtualxsize; + gfx_windowysize = gfx_virtualysize; + + gfx_setclipregion(0, 0, gfx_virtualxsize, gfx_virtualysize); + + gfx_screen = SDL_CreateSurface(xsize, ysize, SDL_PIXELFORMAT_INDEX8); + if (!gfx_screen) + return false; + + gfx_renderer = SDL_CreateRenderer(win_window, nullptr); + sdlTexture = SDL_CreateTexture(gfx_renderer, + SDL_PIXELFORMAT_RGBA32, + SDL_TEXTUREACCESS_STREAMING, + xsize, ysize); + + SDL_HideCursor(); + gfx_setpalette(); + + gfx_initexec = false; + gfx_initted = true; + gfx_redraw = true; + return true; +} + +bool gfx_reinit() +{ + gfx_uninit(); + return gfx_init(gfx_last_xsize, gfx_last_ysize); +} + +void gfx_uninit() +{ + SDL_DestroyTexture(sdlTexture); + SDL_DestroySurface(gfx_screen); + SDL_DestroyRenderer(gfx_renderer); + gfx_initted = false; + return; +} + +bool gfx_lock() +{ + if (gfx_locked) return true; + if (!gfx_initted) return false; + if (SDL_LockSurface(gfx_screen)) + { + gfx_locked = true; + return true; + } + return false; +} + +void gfx_unlock() +{ + if (gfx_locked) + { + SDL_UnlockSurface(gfx_screen); + gfx_locked = false; + } +} + +void gfx_flip() +{ + SDL_Surface* surf = SDL_ConvertSurface(gfx_screen, SDL_PIXELFORMAT_RGBA32); + SDL_UpdateTexture(sdlTexture, nullptr, surf->pixels, surf->pitch); + SDL_DestroySurface(surf); + SDL_RenderClear(gfx_renderer); + SDL_RenderTexture(gfx_renderer, sdlTexture, nullptr, nullptr); + SDL_RenderPresent(gfx_renderer); + gfx_redraw = false; +} + +void gfx_calcpalette() +{ + unsigned char *sptr = gfx_palette; + for (int c = 0; c < MAX_COLORS; c++) + { + gfx_sdlpalette[c].r = *sptr; + sptr++; + + gfx_sdlpalette[c].g = *sptr; + sptr++; - case MOUSE_ALWAYS_HIDDEN: - SDL_HideCursor(); - break; + gfx_sdlpalette[c].b = *sptr; + sptr++; + + gfx_sdlpalette[c].a = 255; } } -void mou_init() +void gfx_setpalette() +{ + if (!gfx_initted) return; + + gfx_calcpalette(); + SDL_Palette *palette = SDL_CreateSurfacePalette(gfx_screen); + SDL_SetPaletteColors(palette, &gfx_sdlpalette[0], 0, MAX_COLORS); +} + +void gfx_setclipregion(unsigned left, unsigned top, unsigned right, unsigned bottom) { - win_mousebuttons = 0; + if (left >= right) return; + if (top >= bottom) return; + if (left >= gfx_virtualxsize) return; + if (top >= gfx_virtualysize) return; + if (right > gfx_virtualxsize) return; + if (bottom > gfx_virtualysize) return; + + gfx_clipleft = left; + gfx_clipright = right; + gfx_cliptop = top; + gfx_clipbottom = bottom; +} + +bool gfx_loadcursor(const char *name) +{ + bme_error = BME_OPEN_ERROR; + + gfx_freecursor(); + + int handle = io_open(name); + if (handle == -1) return false; + + int size = io_lseek(handle, 0, SEEK_END); + io_lseek(handle, 0, SEEK_SET); + char *iconbuffer = new (std::nothrow) char[size]; + io_read(handle, iconbuffer, size); + io_close(handle); + + SDL_IOStream *rw = SDL_IOFromMem(iconbuffer, size); + gfx_cursor = SDL_LoadPNG_IO(rw, true); + if (!gfx_cursor) + return false; + + bme_error = BME_OK; + return true; +} + +void gfx_freecursor() +{ + if (gfx_cursor) + { + SDL_DestroySurface(gfx_cursor); + gfx_cursor = nullptr; + } +} + +void gfx_drawcursor(int x, int y) +{ + if (!gfx_initted) return; + + if (!gfx_cursor) + { + spr_xsize = 0; + spr_ysize = 0; + return; + } + + spr_xsize = gfx_cursor->w; + spr_ysize = gfx_cursor->h; + + if (x >= gfx_clipright) return; + if (y >= gfx_clipbottom) return; + if (x + spr_xsize <= gfx_clipleft) return; + if (y + spr_ysize <= gfx_cliptop) return; + + SDL_Rect dstrect; + dstrect.x = x; + dstrect.y = y; + SDL_BlitSurface(gfx_cursor, NULL, gfx_screen, &dstrect); } void mou_getpos(unsigned *x, unsigned *y) @@ -255,7 +489,7 @@ void mou_getpos(unsigned *x, unsigned *y) } } -unsigned mou_getbuttons(void) +unsigned mou_getbuttons() { return win_mousebuttons; } diff --git a/src/bme/bme_win.h b/src/bme/bme_win.h index 9dcd413..93dacb1 100644 --- a/src/bme/bme_win.h +++ b/src/bme/bme_win.h @@ -5,22 +5,36 @@ #include -int win_openwindow(unsigned xsize, unsigned ysize, const char *appname); -void win_closewindow(void); +#define MAX_COLORS 16 + +bool win_openwindow(unsigned xsize, unsigned ysize, const char *appname); +void win_closewindow(); int win_getspeed(int framerate); -void win_setmousemode(int mode); -void mou_init(); +bool gfx_init(unsigned xsize, unsigned ysize); +bool gfx_lock(); +void gfx_unlock(); +void gfx_flip(); + +void gfx_setpalette(); + +bool gfx_loadcursor(const char *name); +void gfx_drawcursor(int x, int y); +void gfx_freecursor(); + void mou_getpos(unsigned *x, unsigned *y); unsigned mou_getbuttons(); -extern int win_quitted; +extern bool win_quitted; extern int win_fullscreen; extern unsigned char win_keytable[SDL_SCANCODE_COUNT]; extern unsigned char win_keystate[SDL_SCANCODE_COUNT]; extern unsigned char win_asciikey; -extern int win_mousemode; extern float win_mouseywheel; extern SDL_Window *win_window; +extern bool gfx_redraw; +extern unsigned char gfx_palette[]; +extern SDL_Surface *gfx_screen; + #endif diff --git a/src/console.cpp b/src/console.cpp index 174b6c4..996f565 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -28,7 +28,6 @@ #include "bme_main.h" #include "bme_win.h" -#include "bme_gfx.h" #include "bme_io.h" #include @@ -99,14 +98,14 @@ bool initscreen() unsigned xsize = MAX_COLUMNS * 8; unsigned ysize = MAX_ROWS * 16; - win_openwindow(xsize, ysize, "LoadTracker"); - win_setmousemode(MOUSE_ALWAYS_HIDDEN); + if (!win_openwindow(xsize, ysize, "LoadTracker")) + return false; initicon(); - if (!gfx_init(MAX_COLUMNS * fontwidth, MAX_ROWS * fontheight, 60, 0)) + if (!gfx_init(MAX_COLUMNS * fontwidth, MAX_ROWS * fontheight)) { win_fullscreen = 0; - if (!gfx_init(MAX_COLUMNS * fontwidth, MAX_ROWS * fontheight, 60, 0)) + if (!gfx_init(MAX_COLUMNS * fontwidth, MAX_ROWS * fontheight)) return false; } @@ -211,6 +210,7 @@ void closescreen() gfx_freecursor(); gfxinitted = false; + win_closewindow(); } void clearscreen() diff --git a/src/loadtrk.cpp b/src/loadtrk.cpp index 459698f..77fee18 100644 --- a/src/loadtrk.cpp +++ b/src/loadtrk.cpp @@ -186,13 +186,11 @@ void findduplicatepatterns(); int main(int argc, char **argv) { - char filename[MAX_PATHNAME]; - FILE *configfile; - - bool dark = false; - // Open datafile - io_openlinkeddatafile(datafile); + if (!io_openlinkeddatafile(datafile)) + return EXIT_FAILURE; + + char filename[MAX_PATHNAME]; // Load configuration #ifdef __WIN32__ @@ -217,7 +215,7 @@ int main(int argc, char **argv) #endif specialnotenames[0] = 0; scalatuningfilepath[0] = 0; - configfile = fopen(filename, "rt"); + FILE *configfile = fopen(filename, "rt"); if (configfile) { unsigned cfg_version; @@ -262,6 +260,8 @@ int main(int argc, char **argv) // Init pathnames initpaths(); + bool dark = false; + // Scan command line for (int c = 1; c < argc; c++) { @@ -1799,7 +1799,7 @@ void setspecialnotenames() break; if (i < 93) { - char *name = (char*)std::malloc(4); + char *name = (char*)std::malloc(4); // FIXME std::strncpy(name, specialnotenames + j, 2); std::sprintf(octave, "%d", oct); std::strcpy(name + 2, octave); @@ -1813,16 +1813,13 @@ void setspecialnotenames() void readscalatuningfile() { - char *configptr; - char strbuf[64]; - char name[3]; - double numerator; - double denominator; - double centvalue; - FILE *scalatuningfile = fopen(scalatuningfilepath, "rt"); if (scalatuningfile) { + char *configptr; + char strbuf[64]; + char name[3]; + // Tuning name for (;;) { @@ -1870,13 +1867,15 @@ void readscalatuningfile() } else { - strcat(specialnotenames, name); + std::strcat(specialnotenames, name); } } if (!strchr(strbuf, '.')) { + double numerator; + double denominator; std::sscanf(strbuf, "%lf", &numerator); - if (strchr(strbuf, '/')) + if (std::strchr(strbuf, '/')) { std::sscanf(strchr(strbuf, '/') + 1, "%lf", &denominator); tuning[i] = numerator / denominator; @@ -1884,6 +1883,7 @@ void readscalatuningfile() } else { + double centvalue; std::sscanf(configptr, "%lf", ¢value); tuning[i] = std::pow(2.0, centvalue / 1200.0); } @@ -1895,8 +1895,6 @@ void readscalatuningfile() void switchMode() { char nextMode[7]; - char textbuffer[80]; - if (numsids == 1) { std::strcpy(nextMode, "STEREO"); @@ -1906,6 +1904,7 @@ void switchMode() std::strcpy(nextMode, "MONO"); } + char textbuffer[80]; std::sprintf(textbuffer, "Switch to %s Mode (y/n) ?", nextMode); printtextcp( @@ -1931,7 +1930,7 @@ void switchMode() std::memset(songfilename, 0, sizeof songfilename); numsids ^= 3; - clearsong(1, 1, 1, 1, 1); + clearsong(true, true, true, true, true); sound_init( mr,