diff --git a/CMakeLists.txt b/CMakeLists.txt index 7940522..9c6e029 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,21 +17,21 @@ if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU" OR endif() set(LOADTRK_SOURCES - ${CMAKE_BINARY_DIR}/src/goatdata.c - src/loadtrk.c - src/gsong.c - src/gorder.c - src/gpattern.c - src/ginstr.c - src/gtable.c - src/gplay.c - src/gdisplay.c - src/gfile.c - src/greloc.c - src/ghelp.c - src/gsound.c - src/gconsole.c - src/gsid.cpp + ${CMAKE_BINARY_DIR}/src/ltrkdata.cpp + src/loadtrk.cpp + src/song.cpp + src/order.cpp + src/pattern.cpp + src/instr.cpp + src/table.cpp + src/play.cpp + src/display.cpp + src/file.cpp + src/reloc.cpp + src/help.cpp + src/sound.cpp + src/console.cpp + src/sid.cpp src/asm/asmtab.c src/asm/chnkpool.c src/asm/expr.c @@ -43,13 +43,11 @@ set(LOADTRK_SOURCES src/asm/parse.c src/asm/pc.c src/asm/vec.c - src/bme/bme_gfx.c - src/bme/bme_snd.c - src/bme/bme_win.c - src/bme/bme_mou.c - src/bme/bme_io.c - src/bme/bme_end.c - src/bme/bme.c + src/bme/bme_gfx.cpp + src/bme/bme_snd.cpp + src/bme/bme_win.cpp + src/bme/bme_io.cpp + src/bme/bme_end.cpp src/rtmidi/rtmidi_c.h src/rtmidi/rtmidi_c.cpp src/rtmidi/RtMidi.h @@ -62,29 +60,39 @@ if(MINGW) endif(MINGW) set(MOD2SNG_SOURCES - src/mod2sng.c - src/bme/bme_end.c) + src/tools/mod2sng.cpp + src/bme/bme_end.cpp) set(INS2SND2_SOURCES - src/ins2snd2.c - src/bme/bme_end.c) + src/tools/ins2snd2.cpp + src/bme/bme_end.cpp) set(SNGSPLI2_SOURCES - src/sngspli2.c - src/bme/bme_end.c) + src/tools/sngspli2.cpp + src/bme/bme_end.cpp) + +set(DATAFILE_SOURCES + src/tools/datafile.cpp + src/bme/bme_end.cpp) + +set(DAT2INC_SOURCES + src/tools/dat2inc.cpp) include_directories(${CMAKE_SOURCE_DIR}/src) include_directories(${CMAKE_SOURCE_DIR}/src/bme) include_directories(${CMAKE_SOURCE_DIR}/src/asm) -include_directories(${CMAKE_BINARY_DIR}/src/residfp) find_package(SDL3 REQUIRED) -add_executable(datafile src/bme/datafile.c src/bme/bme_end.c) -add_executable(dat2inc src/bme/dat2inc.c) +add_executable(dat2inc ${DAT2INC_SOURCES}) +add_executable(datafile ${DATAFILE_SOURCES}) +add_executable(loadtrk ${LOADTRK_SOURCES}) +add_executable(mod2sng ${MOD2SNG_SOURCES}) +add_executable(ins2snd2 ${INS2SND2_SOURCES}) +add_executable(sngspli2 ${SNGSPLI2_SOURCES}) -target_include_directories(datafile PUBLIC SDL3::Headers) +target_include_directories(datafile PUBLIC SDL3::Headers) file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/src) @@ -96,19 +104,14 @@ add_custom_command( ) add_custom_command( - OUTPUT ${CMAKE_BINARY_DIR}/src/goatdata.c - COMMAND dat2inc loadtrk.dat goatdata.c + OUTPUT ${CMAKE_BINARY_DIR}/src/ltrkdata.cpp + COMMAND dat2inc loadtrk.dat ltrkdata.cpp DEPENDS dat2inc ${CMAKE_BINARY_DIR}/src/loadtrk.dat WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/src ) -add_executable(loadtrk ${LOADTRK_SOURCES}) -add_executable(mod2sng ${MOD2SNG_SOURCES}) -add_executable(ins2snd2 ${INS2SND2_SOURCES}) -add_executable(sngspli2 ${SNGSPLI2_SOURCES}) - -add_custom_target(goatdata DEPENDS ${CMAKE_BINARY_DIR}/src/goatdata.c) -add_dependencies(loadtrk goatdata) +add_custom_target(ltrkdata DEPENDS ${CMAKE_BINARY_DIR}/src/ltrkdata.cpp) +add_dependencies(loadtrk ltrkdata) target_include_directories(mod2sng PUBLIC SDL3::Headers) target_include_directories(ins2snd2 PUBLIC SDL3::Headers) diff --git a/src/bcursor.hex b/src/bcursor.hex deleted file mode 100644 index 1ba8ada..0000000 --- a/src/bcursor.hex +++ /dev/null @@ -1,42 +0,0 @@ -01 00 00 00 16 00 28 00 00 00 00 00 00 00 00 00 -04 00 00 00 00 FF -04 00 00 00 00 FF -06 00 00 FF FF 00 00 FF -06 00 00 FF FF 00 00 FF -08 00 00 FF FF FF FF 00 00 FF -08 00 00 FF FF FF FF 00 00 FF -0A 00 00 FF FF FF FF FF FF 00 00 FF -0A 00 00 FF FF FF FF FF FF 00 00 FF -0C 00 00 FF FF FF FF FF FF FF FF 00 00 FF -0C 00 00 FF FF FF FF FF FF FF FF 00 00 FF -0E 00 00 FF FF FF FF FF FF FF FF FF FF 00 00 FF -0E 00 00 FF FF FF FF FF FF FF FF FF FF 00 00 FF -10 00 00 FF FF FF FF FF FF FF FF FF FF FF FF 00 00 FF -10 00 00 FF FF FF FF FF FF FF FF FF FF FF FF 00 00 FF -12 00 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF 00 00 FF -12 00 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF 00 00 FF -14 00 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 00 00 FF -14 00 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 00 00 FF -16 00 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 00 00 FF -16 00 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 00 00 FF -18 00 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 00 00 FF -18 00 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 00 00 FF -10 00 00 FF FF FF FF FF FF FF FF FF FF FF FF 00 00 FF -10 00 00 FF FF FF FF FF FF FF FF FF FF FF FF 00 00 FF -10 00 00 FF FF FF FF FF FF 00 00 FF FF FF FF 00 00 FF -10 00 00 FF FF FF FF FF FF 00 00 FF FF FF FF 00 00 FF -08 00 00 FF FF 00 00 00 00 82 08 00 00 FF FF FF FF 00 00 FF -08 00 00 FF FF 00 00 00 00 82 08 00 00 FF FF FF FF 00 00 FF -04 00 00 00 00 86 08 00 00 FF FF FF FF 00 00 FF -04 00 00 00 00 86 08 00 00 FF FF FF FF 00 00 FF -8C 08 00 00 FF FF FF FF 00 00 FF -8C 08 00 00 FF FF FF FF 00 00 FF -8C 08 00 00 FF FF FF FF 00 00 FF -8C 08 00 00 FF FF FF FF 00 00 FF -8E 08 00 00 FF FF FF FF 00 00 FF -8E 08 00 00 FF FF FF FF 00 00 FF -8E 08 00 00 FF FF FF FF 00 00 FF -8E 08 00 00 FF FF FF FF 00 00 FF -90 04 00 00 00 00 FF -90 04 00 00 00 00 FF -00 \ No newline at end of file diff --git a/src/bme/bme.c b/src/bme/bme.c deleted file mode 100644 index 2338bbe..0000000 --- a/src/bme/bme.c +++ /dev/null @@ -1,8 +0,0 @@ -// -// BME (Blasphemous Multimedia Engine) main module -// - -#include "bme_err.h" -#include "bme_cfg.h" - -int bme_error = BME_OK; diff --git a/src/bme/bme.h b/src/bme/bme.h deleted file mode 100644 index e479815..0000000 --- a/src/bme/bme.h +++ /dev/null @@ -1,17 +0,0 @@ -// BME header file for user applications - -#ifndef BME_H -#define BME_H - -#include -#include "bme_main.h" -#include "bme_cfg.h" -#include "bme_err.h" -#include "bme_snd.h" -#include "bme_gfx.h" -#include "bme_win.h" -#include "bme_mou.h" -#include "bme_io.h" -#include "bme_end.h" - -#endif diff --git a/src/bme/bme_cfg.h b/src/bme/bme_cfg.h deleted file mode 100644 index d6e03ba..0000000 --- a/src/bme/bme_cfg.h +++ /dev/null @@ -1,11 +0,0 @@ -// BME configuration (some static things) - -#define MAX_JOYSTICKS 16 // Maximum joysticks -#define MAX_HANDLES 16 // Up to 16 simultaneous files open from the - // datafile - -#define MAX_COLORS 256 // 8bit oldskool mode - -#define DEFAULT_MAX_SPRFILES 256 // Default maximum of 256 spritefiles, though - // this can be set during running of program - diff --git a/src/bme/bme_end.c b/src/bme/bme_end.c deleted file mode 100644 index f3fc51b..0000000 --- a/src/bme/bme_end.c +++ /dev/null @@ -1,72 +0,0 @@ -#include -#include - -void fwrite8(FILE *file, unsigned data) -{ - uint8_t bytes[1]; - - bytes[0] = data; - fwrite(bytes, 1, 1, file); -} - -void fwritele16(FILE *file, unsigned data) -{ - uint8_t bytes[2]; - - bytes[0] = data; - bytes[1] = data >> 8; - fwrite(bytes, 2, 1, file); -} - -void fwritele32(FILE *file, unsigned data) -{ - uint8_t bytes[4]; - - bytes[0] = data; - bytes[1] = data >> 8; - bytes[2] = data >> 16; - bytes[3] = data >> 24; - fwrite(bytes, 4, 1, file); -} - -unsigned fread8(FILE *file) -{ - uint8_t bytes[1]; - - fread(bytes, 1, 1, file); - return bytes[0]; -} - -unsigned freadle16(FILE *file) -{ - uint8_t bytes[2]; - - fread(bytes, 2, 1, file); - return (bytes[0]) | (bytes[1] << 8); -} - -unsigned freadle32(FILE *file) -{ - uint8_t bytes[4]; - - fread(bytes, 4, 1, file); - return (bytes[0]) | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24); -} - -unsigned freadhe16(FILE *file) -{ - uint8_t bytes[2]; - - fread(bytes, 2, 1, file); - return (bytes[1]) | (bytes[0] << 8); -} - -unsigned freadhe32(FILE *file) -{ - uint8_t bytes[4]; - - fread(bytes, 4, 1, file); - return (bytes[3]) | (bytes[2] << 8) | (bytes[1] << 16) | (bytes[0] << 24); -} - - diff --git a/src/bme/bme_end.cpp b/src/bme/bme_end.cpp new file mode 100644 index 0000000..fbd2743 --- /dev/null +++ b/src/bme/bme_end.cpp @@ -0,0 +1,38 @@ +#include +#include + +void fwrite8(FILE *file, unsigned data) +{ + uint8_t bytes[1]; + + bytes[0] = data; + std::fwrite(bytes, 1, 1, file); +} + +void fwritele16(FILE *file, unsigned data) +{ + uint8_t bytes[2]; + + bytes[0] = data; + bytes[1] = data >> 8; + std::fwrite(bytes, 2, 1, file); +} + +void fwritele32(FILE *file, unsigned data) +{ + uint8_t bytes[4]; + + bytes[0] = data; + bytes[1] = data >> 8; + bytes[2] = data >> 16; + bytes[3] = data >> 24; + std::fwrite(bytes, 4, 1, file); +} + +unsigned fread8(FILE *file) +{ + uint8_t bytes[1]; + + std::fread(bytes, 1, 1, file); + return bytes[0]; +} diff --git a/src/bme/bme_end.h b/src/bme/bme_end.h index 31dbafd..5eb0d85 100644 --- a/src/bme/bme_end.h +++ b/src/bme/bme_end.h @@ -1,8 +1,12 @@ + +#ifndef BME_END_H +#define BME_END_H + +#include + void fwrite8(FILE *file, unsigned data); void fwritele16(FILE *file, unsigned data); void fwritele32(FILE *file, unsigned data); unsigned fread8(FILE *file); -unsigned freadle16(FILE *file); -unsigned freadle32(FILE *file); -unsigned freadhe16(FILE *file); -unsigned freadhe32(FILE *file); + +#endif diff --git a/src/bme/bme_err.h b/src/bme/bme_err.h deleted file mode 100644 index dc47ce8..0000000 --- a/src/bme/bme_err.h +++ /dev/null @@ -1,19 +0,0 @@ -// BME error code header file - -// BME function return codes - -#define BME_OK 1 -#define BME_ERROR 0 - -// BME detailed error types (in bme_error) - -#define BME_OPEN_ERROR -1 -#define BME_READ_ERROR -2 -#define BME_WRONG_FORMAT -3 -#define BME_OUT_OF_MEMORY -4 -#define BME_THREAD_ERROR -5 -#define BME_ILLEGAL_CONFIG -6 -#define BME_OUT_OF_CHANNELS -7 -#define BME_GRAPHICS_ERROR -8 -#define BME_SOUND_ERROR -9 - diff --git a/src/bme/bme_gfx.c b/src/bme/bme_gfx.c deleted file mode 100644 index 9bce18f..0000000 --- a/src/bme/bme_gfx.c +++ /dev/null @@ -1,581 +0,0 @@ -// -// BME (Blasphemous Multimedia Engine) graphics main module -// - -#include -#include -#include -#include - -#include "bme_main.h" -#include "bme_cfg.h" -#include "bme_win.h" -#include "bme_io.h" -#include "bme_err.h" - -// Prototypes - -int gfx_init(unsigned xsize, unsigned ysize, unsigned framerate, unsigned flags); -int gfx_reinit(void); -void gfx_uninit(void); -int gfx_lock(void); -void gfx_unlock(void); -void gfx_flip(void); -void gfx_setclipregion(unsigned left, unsigned top, unsigned right, unsigned bottom); -void gfx_setmaxspritefiles(unsigned num); -void gfx_setmaxcolors(int num); -int gfx_loadpalette(const char *name); -void gfx_calcpalette(int fade, int radd, int gadd, int badd); -void gfx_setpalette(void); -int gfx_loadblocks(char *name); -int gfx_loadsprites(unsigned num, const char *name); -void gfx_freesprites(unsigned num); - -void gfx_drawsprite(int x, int y, unsigned num); -void gfx_getspriteinfo(unsigned num); - -int gfx_initted = 0; -int gfx_redraw = 0; -int gfx_scanlinemode = 0; -int gfx_preventswitch = 0; -unsigned gfx_virtualxsize; -unsigned gfx_virtualysize; -unsigned gfx_windowxsize; -unsigned gfx_windowysize; -int gfx_blockxsize = 16; -int gfx_blockysize = 16; -int spr_xsize = 0; -int spr_ysize = 0; -int spr_xhotspot = 0; -int spr_yhotspot = 0; -unsigned gfx_nblocks = 0; -Uint8 gfx_palette[MAX_COLORS * 3] = {0}; -SDL_Surface *gfx_screen = NULL; -SDL_Renderer *gfx_renderer = NULL; - -// Static variables - -static int gfx_initexec = 0; -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 int gfx_maxcolors = MAX_COLORS; -static unsigned gfx_maxspritefiles = 0; -static SPRITEHEADER **gfx_spriteheaders = NULL; -static Uint8 **gfx_spritedata = NULL; -static unsigned *gfx_spriteamount = NULL; -static SDL_Color gfx_sdlpalette[MAX_COLORS]; -static int gfx_locked = 0; -static SDL_Texture *sdlTexture = NULL; - -int gfx_init(unsigned xsize, unsigned ysize, unsigned framerate, unsigned flags) -{ - // Prevent re-entry (by window procedure) - if (gfx_initexec) return BME_OK; - gfx_initexec = 1; - - 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); - - if (flags & GFX_NOSWITCHING) gfx_preventswitch = 1; - else gfx_preventswitch = 0; - - 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 = 0; - gfx_uninit(); - bme_error = BME_ILLEGAL_CONFIG; - return BME_ERROR; - } - - // 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); - - // Colors 0 & 255 are always black & white - gfx_sdlpalette[0].r = 0; - gfx_sdlpalette[0].g = 0; - gfx_sdlpalette[0].b = 0; - gfx_sdlpalette[0].a = 255; - gfx_sdlpalette[255].r = 255; - gfx_sdlpalette[255].g = 255; - gfx_sdlpalette[255].b = 255; - gfx_sdlpalette[255].a = 255; - - gfx_renderer = SDL_CreateRenderer(win_window, NULL); - gfx_screen = SDL_CreateSurface(xsize, ysize, SDL_PIXELFORMAT_INDEX8); - sdlTexture = SDL_CreateTexture(gfx_renderer, - SDL_PIXELFORMAT_RGBA32, - SDL_TEXTUREACCESS_STREAMING, - xsize, ysize); - - gfx_initexec = 0; - if (gfx_screen) - { - gfx_initted = 1; - gfx_redraw = 1; - gfx_setpalette(); - win_setmousemode(win_mousemode); - return BME_OK; - } - else return BME_ERROR; -} - -int gfx_reinit(void) -{ - gfx_uninit(); - return gfx_init(gfx_last_xsize, gfx_last_ysize, gfx_last_framerate, - gfx_last_flags); -} - -void gfx_uninit(void) -{ - SDL_DestroyTexture(sdlTexture); - SDL_DestroySurface(gfx_screen); - SDL_DestroyRenderer(gfx_renderer); - gfx_initted = 0; - return; -} - -int gfx_lock(void) -{ - if (gfx_locked) return 1; - if (!gfx_initted) return 0; - if (SDL_LockSurface(gfx_screen)) - { - gfx_locked = 1; - return 1; - } - else return 0; -} - -void gfx_unlock(void) -{ - if (gfx_locked) - { - SDL_UnlockSurface(gfx_screen); - gfx_locked = 0; - } -} - -void gfx_flip() -{ - SDL_Surface* surf = SDL_ConvertSurface(gfx_screen, SDL_PIXELFORMAT_RGBA32); - SDL_UpdateTexture(sdlTexture, NULL, surf->pixels, surf->pitch); - SDL_DestroySurface(surf); - SDL_RenderClear(gfx_renderer); - SDL_RenderTexture(gfx_renderer, sdlTexture, NULL, NULL); - SDL_RenderPresent(gfx_renderer); - gfx_redraw = 0; -} - -void gfx_setmaxcolors(int num) -{ - gfx_maxcolors = num; -} - -int gfx_loadpalette(const char *name) -{ - int handle; - - handle = io_open(name); - if (handle == -1) - { - bme_error = BME_OPEN_ERROR; - return BME_ERROR; - } - if (io_read(handle, gfx_palette, sizeof gfx_palette) != sizeof gfx_palette) - { - bme_error = BME_READ_ERROR; - io_close(handle); - return BME_ERROR; - } - - io_close(handle); - gfx_calcpalette(64, 0, 0, 0); - bme_error = BME_OK; - return BME_OK; -} - -void gfx_calcpalette(int fade, int radd, int gadd, int badd) -{ - Uint8 *sptr = &gfx_palette[3]; - int c, cl; - if (radd < 0) radd = 0; - if (gadd < 0) gadd = 0; - if (badd < 0) badd = 0; - - for (c = 1; c < 255; c++) - { - cl = *sptr; - cl *= fade; - cl >>= 6; - cl += radd; - if (cl > 63) cl = 63; - if (cl < 0) cl = 0; - gfx_sdlpalette[c].r = (cl << 2) | (cl & 3); - sptr++; - - cl = *sptr; - cl *= fade; - cl >>= 6; - cl += gadd; - if (cl > 63) cl = 63; - if (cl < 0) cl = 0; - gfx_sdlpalette[c].g = (cl << 2) | (cl & 3); - sptr++; - - cl = *sptr; - cl *= fade; - cl >>= 6; - cl += badd; - if (cl > 63) cl = 63; - if (cl < 0) cl = 0; - gfx_sdlpalette[c].b = (cl << 2) | (cl & 3); - sptr++; - - gfx_sdlpalette[c].a = 255; - } -} - -void gfx_setpalette(void) -{ - if (!gfx_initted) return; - - SDL_Palette *palette = SDL_CreateSurfacePalette(gfx_screen); - SDL_SetPaletteColors(palette, &gfx_sdlpalette[0], 0, gfx_maxcolors); -} - -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; -} - -void gfx_setmaxspritefiles(unsigned num) -{ - if (gfx_spriteheaders) return; - - gfx_spriteheaders = malloc(num * sizeof(Uint8 *)); - gfx_spritedata = malloc(num * sizeof(Uint8 *)); - gfx_spriteamount = malloc(num * sizeof(unsigned)); - if ((gfx_spriteheaders) && (gfx_spritedata) && (gfx_spriteamount)) - { - unsigned c; - - gfx_maxspritefiles = num; - for (c = 0; c < num; c++) - { - gfx_spriteamount[c] = 0; - gfx_spritedata[c] = NULL; - gfx_spriteheaders[c] = NULL; - } - } - else gfx_maxspritefiles = 0; -} - -int gfx_loadsprites(unsigned num, const char *name) -{ - unsigned c; - int handle, size; - int datastart; - - if (!gfx_spriteheaders) - { - gfx_setmaxspritefiles(DEFAULT_MAX_SPRFILES); - } - - bme_error = BME_OPEN_ERROR; - if (num >= gfx_maxspritefiles) return BME_ERROR; - - gfx_freesprites(num); - - handle = io_open(name); - if (handle == -1) return BME_ERROR; - - size = io_lseek(handle, 0, SEEK_END); - io_lseek(handle, 0, SEEK_SET); - - gfx_spriteamount[num] = io_readle32(handle); - - gfx_spriteheaders[num] = malloc(gfx_spriteamount[num] * sizeof(SPRITEHEADER)); - - if (!gfx_spriteheaders[num]) - { - bme_error = BME_OUT_OF_MEMORY; - io_close(handle); - return BME_ERROR; - } - - for (c = 0; c < gfx_spriteamount[num]; c++) - { - SPRITEHEADER *hptr = gfx_spriteheaders[num] + c; - - hptr->xsize = io_readle16(handle); - hptr->ysize = io_readle16(handle); - hptr->xhot = io_readle16(handle); - hptr->yhot = io_readle16(handle); - hptr->offset = io_readle32(handle); - } - - datastart = io_lseek(handle, 0, SEEK_CUR); - gfx_spritedata[num] = malloc(size - datastart); - if (!gfx_spritedata[num]) - { - bme_error = BME_OUT_OF_MEMORY; - io_close(handle); - return BME_ERROR; - } - io_read(handle, gfx_spritedata[num], size - datastart); - io_close(handle); - bme_error = BME_OK; - return BME_OK; -} - -void gfx_freesprites(unsigned num) -{ - if (num >= gfx_maxspritefiles) return; - - if (gfx_spritedata[num]) - { - free(gfx_spritedata[num]); - gfx_spritedata[num] = NULL; - } - if (gfx_spriteheaders[num]) - { - free(gfx_spriteheaders[num]); - gfx_spriteheaders[num] = NULL; - } -} - -void gfx_copyscreen8(Uint8 *destaddress, Uint8 *srcaddress, unsigned pitch) -{ - unsigned c; - int d; - - switch(gfx_scanlinemode) - { - default: - for (c = 0; c < gfx_virtualysize; c++) - { - memcpy(destaddress, srcaddress, gfx_virtualxsize); - destaddress += pitch; - srcaddress += gfx_virtualxsize; - } - break; - - case GFX_SCANLINES: - for (c = 0; c < gfx_virtualysize; c++) - { - d = gfx_virtualxsize; - while (d--) - { - *destaddress = *srcaddress; - destaddress++; - *destaddress = *srcaddress; - destaddress++; - srcaddress++; - } - destaddress += pitch*2 - (gfx_virtualxsize << 1); - } - break; - - case GFX_DOUBLESIZE: - for (c = 0; c < gfx_virtualysize; c++) - { - 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_getspriteinfo(unsigned num) -{ - unsigned sprf = num >> 16; - unsigned spr = (num & 0xffff) - 1; - SPRITEHEADER *hptr; - - if ((sprf >= gfx_maxspritefiles) || (!gfx_spriteheaders[sprf]) || - (spr >= gfx_spriteamount[sprf])) hptr = NULL; - else hptr = gfx_spriteheaders[sprf] + spr; - - if (!hptr) - { - spr_xsize = 0; - spr_ysize = 0; - spr_xhotspot = 0; - spr_yhotspot = 0; - return; - } - - spr_xsize = hptr->xsize; - spr_ysize = hptr->ysize; - spr_xhotspot = hptr->xhot; - spr_yhotspot = hptr->yhot; -} - -void gfx_drawsprite(int x, int y, unsigned num) -{ - unsigned sprf = num >> 16; - unsigned spr = (num & 0xffff) - 1; - SPRITEHEADER *hptr; - - Uint8 *sptr; - Uint8 *dptr; - int cx; - - if (!gfx_initted) return; - if (!gfx_locked) return; - - if ((sprf >= gfx_maxspritefiles) || (!gfx_spriteheaders[sprf]) || - (spr >= gfx_spriteamount[sprf])) - { - spr_xsize = 0; - spr_ysize = 0; - spr_xhotspot = 0; - spr_yhotspot = 0; - return; - } - else hptr = gfx_spriteheaders[sprf] + spr; - - sptr = gfx_spritedata[sprf] + hptr->offset; - spr_xsize = hptr->xsize; - spr_ysize = hptr->ysize; - spr_xhotspot = hptr->xhot; - spr_yhotspot = hptr->yhot; - - x -= spr_xhotspot; - y -= spr_yhotspot; - - if (x >= gfx_clipright) return; - if (y >= gfx_clipbottom) return; - if (x + spr_xsize <= gfx_clipleft) return; - if (y + spr_ysize <= gfx_cliptop) return; - - while (y < gfx_cliptop) - { - int dec = *sptr++; - if (dec == 255) - { - if (!(*sptr)) return; - y++; - } - else - { - if (dec < 128) - { - sptr += dec; - } - } - } - while (y < gfx_clipbottom) - { - int dec; - cx = x; - dptr = (Uint8*)gfx_screen->pixels + y * gfx_screen->pitch + x; - - for (;;) - { - dec = *sptr++; - - if (dec == 255) - { - if (!(*sptr)) return; - y++; - break; - } - if (dec < 128) - { - if ((cx + dec <= gfx_clipleft) || (cx >= gfx_clipright)) - { - goto SKIP; - } - if (cx < gfx_clipleft) - { - dec -= (gfx_clipleft - cx); - sptr += (gfx_clipleft - cx); - dptr += (gfx_clipleft - cx); - cx = gfx_clipleft; - } - while ((cx < gfx_clipright) && (dec)) - { - *dptr = *sptr; - cx++; - sptr++; - dptr++; - dec--; - } - SKIP: - cx += dec; - sptr += dec; - dptr += dec; - } - else - { - cx += (dec & 0x7f); - dptr += (dec & 0x7f); - } - } - } -} - - diff --git a/src/bme/bme_gfx.cpp b/src/bme/bme_gfx.cpp new file mode 100644 index 0000000..d855398 --- /dev/null +++ b/src/bme/bme_gfx.cpp @@ -0,0 +1,377 @@ +// +// 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 + +#define MAX_COLORS 256 // 8bit oldskool mode + +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; +Uint8 gfx_palette[MAX_COLORS * 3] = {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 int gfx_maxcolors = MAX_COLORS; + +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); + + // Colors 0 & 255 are always black & white + gfx_sdlpalette[0].r = 0; + gfx_sdlpalette[0].g = 0; + gfx_sdlpalette[0].b = 0; + gfx_sdlpalette[0].a = 255; + gfx_sdlpalette[255].r = 255; + gfx_sdlpalette[255].g = 255; + gfx_sdlpalette[255].b = 255; + gfx_sdlpalette[255].a = 255; + + 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; +} + +bool gfx_loadpalette(const char *name) +{ + int handle = io_open(name); + if (handle == -1) + { + bme_error = BME_OPEN_ERROR; + return false; + } + if (io_read(handle, gfx_palette, sizeof gfx_palette) != sizeof gfx_palette) + { + bme_error = BME_READ_ERROR; + io_close(handle); + return false; + } + + io_close(handle); + gfx_calcpalette(64, 0, 0, 0); + bme_error = BME_OK; + return true; +} + +void gfx_calcpalette(int fade, int radd, int gadd, int badd) +{ + if (radd < 0) radd = 0; + if (gadd < 0) gadd = 0; + if (badd < 0) badd = 0; + + Uint8 *sptr = &gfx_palette[3]; + for (int c = 1; c < 255; c++) + { + int cl = *sptr; + cl *= fade; + cl >>= 6; + cl += radd; + if (cl > 63) cl = 63; + if (cl < 0) cl = 0; + gfx_sdlpalette[c].r = (cl << 2) | (cl & 3); + sptr++; + + cl = *sptr; + cl *= fade; + cl >>= 6; + cl += gadd; + if (cl > 63) cl = 63; + if (cl < 0) cl = 0; + gfx_sdlpalette[c].g = (cl << 2) | (cl & 3); + sptr++; + + cl = *sptr; + cl *= fade; + cl >>= 6; + cl += badd; + if (cl > 63) cl = 63; + if (cl < 0) cl = 0; + gfx_sdlpalette[c].b = (cl << 2) | (cl & 3); + sptr++; + + gfx_sdlpalette[c].a = 255; + } +} + +void gfx_setpalette() +{ + if (!gfx_initted) return; + + SDL_Palette *palette = SDL_CreateSurfacePalette(gfx_screen); + SDL_SetPaletteColors(palette, &gfx_sdlpalette[0], 0, gfx_maxcolors); +} + +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 index 1302e89..acbe0c2 100644 --- a/src/bme/bme_gfx.h +++ b/src/bme/bme_gfx.h @@ -1,41 +1,32 @@ // BME graphics module header file -int gfx_init(unsigned xsize, unsigned ysize, unsigned framerate, unsigned flags); -int gfx_reinit(void); -void gfx_uninit(void); -int gfx_lock(void); -void gfx_unlock(void); -void gfx_flip(void); -void gfx_setclipregion(unsigned left, unsigned top, unsigned right, unsigned bottom); -void gfx_setmaxspritefiles(unsigned num); -void gfx_setmaxcolors(int num); -int gfx_loadpalette(const char *name); +#ifndef BME_GFX_H +#define BME_GFX_H + +#include + +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(); + +bool gfx_loadpalette(const char *name); void gfx_calcpalette(int fade, int radd, int gadd, int badd); -void gfx_setpalette(void); -int gfx_loadblocks(char *name); -int gfx_loadsprites(unsigned num, const char *name); -void gfx_freesprites(unsigned num); +void gfx_setpalette(); -void gfx_drawsprite(int x, int y, unsigned num); +bool gfx_loadcursor( const char *name); +void gfx_drawcursor(int x, int y); +void gfx_freecursor(); -extern int gfx_initted; -extern int gfx_scanlinemode; -extern int gfx_preventswitch; -extern int gfx_fullscreen; -extern int gfx_redraw; +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 gfx_nblocks; -extern int gfx_blockxsize; -extern int gfx_blockysize; -extern int spr_xsize; -extern int spr_ysize; -extern int spr_xhotspot; -extern int spr_yhotspot; -extern Uint8 *gfx_vscreen; -extern Uint8 *gfx_blocks; extern Uint8 gfx_palette[]; extern SDL_Surface *gfx_screen; -extern SDL_Renderer *gfx_renderer; + +#endif diff --git a/src/bme/bme_io.c b/src/bme/bme_io.c deleted file mode 100644 index 8e98701..0000000 --- a/src/bme/bme_io.c +++ /dev/null @@ -1,353 +0,0 @@ -// -// BME (Blasphemous Multimedia Engine) datafile IO main module -// - -#include -#include -#include -#include -#include - -#include "bme_main.h" -#include "bme_err.h" -#include "bme_cfg.h" - -typedef struct -{ - Uint32 offset; - Sint32 length; - char name[13]; -} HEADER; - -typedef struct -{ - HEADER *currentheader; - int filepos; - int open; -} HANDLE; - -static int io_usedatafile = 0; -static HEADER *fileheaders; -static unsigned files; -static char ident[4]; -static char *idstring = "DAT!"; -static HANDLE handle[MAX_HANDLES]; -static FILE *fileptr[MAX_HANDLES] = {NULL}; -static FILE *datafilehandle = NULL; -static unsigned char *datafileptr; -static unsigned char *datafilestart; - -static unsigned freadle32(FILE *index); -static void linkedseek(unsigned pos); -static void linkedread(void *buffer, int length); -static unsigned linkedreadle32(void); - -void io_setfilemode(int usedf) -{ - io_usedatafile = usedf; -} - -int io_openlinkeddatafile(unsigned char *ptr) -{ - unsigned index; - - if (datafilehandle) fclose(datafilehandle); - datafilehandle = NULL; - - datafilestart = ptr; - linkedseek(0); - - linkedread(ident, 4); - if (memcmp(ident, idstring, 4)) - { - bme_error = BME_WRONG_FORMAT; - return BME_ERROR; - } - - files = linkedreadle32(); - fileheaders = malloc(files * sizeof(HEADER)); - if (!fileheaders) - { - bme_error = BME_OUT_OF_MEMORY; - return BME_ERROR; - } - for (index = 0; index < files; index++) - { - fileheaders[index].offset = linkedreadle32(); - fileheaders[index].length = linkedreadle32(); - linkedread(&fileheaders[index].name, 13); - } - - for (index = 0; index < MAX_HANDLES; index++) handle[index].open = 0; - io_usedatafile = 1; - bme_error = BME_OK; - return BME_OK; -} - -int io_opendatafile(char *name) -{ - unsigned index; - - if (name) - { - datafilehandle = fopen(name, "rb"); - if (!datafilehandle) - { - bme_error = BME_OPEN_ERROR; - return BME_ERROR; - } - } - - fread(ident, 4, 1, datafilehandle); - if (memcmp(ident, idstring, 4)) - { - bme_error = BME_WRONG_FORMAT; - return BME_ERROR; - } - - files = freadle32(datafilehandle); - fileheaders = malloc(files * sizeof(HEADER)); - if (!fileheaders) - { - bme_error = BME_OUT_OF_MEMORY; - return BME_ERROR; - } - for (index = 0; index < files; index++) - { - fileheaders[index].offset = freadle32(datafilehandle); - fileheaders[index].length = freadle32(datafilehandle); - fread(&fileheaders[index].name, 13, 1, datafilehandle); - } - - for (index = 0; index < MAX_HANDLES; index++) handle[index].open = 0; - io_usedatafile = 1; - bme_error = BME_OK; - return BME_OK; -} - -// Returns nonnegative file handle if successful, -1 on error - -int io_open(const char *name) -{ - if (!name) return -1; - - if (!io_usedatafile) - { - int index; - for (index = 0; index < MAX_HANDLES; index++) - { - if (!fileptr[index]) break; - } - if (index == MAX_HANDLES) return -1; - else - { - FILE *file = fopen(name, "rb"); - if (!file) - { - return -1; - } - else - { - fileptr[index] = file; - return index; - } - } - } - else - { - size_t index; - size_t namelength; - char namecopy[13]; - - namelength = strlen(name); - if (namelength > 12) namelength = 12; - memcpy(namecopy, name, namelength + 1); - for (index = 0; index < strlen(namecopy); index++) - { - namecopy[index] = toupper(namecopy[index]); - } - - for (index = 0; index < MAX_HANDLES; index++) - { - if (!handle[index].open) - { - int count = files; - handle[index].currentheader = fileheaders; - - while (count) - { - if (!strcmp(namecopy, handle[index].currentheader->name)) - { - handle[index].open = 1; - handle[index].filepos = 0; - return index; - } - count--; - handle[index].currentheader++; - } - return -1; - } - } - return -1; - } -} - -// Returns file position after seek or -1 on error - -int io_lseek(int index, int offset, int whence) -{ - if (!io_usedatafile) - { - fseek(fileptr[index], offset, whence); - return ftell(fileptr[index]); - } - else - { - int newpos; - - if ((index < 0) || (index >= MAX_HANDLES)) return -1; - - if (!handle[index].open) return -1; - switch(whence) - { - default: - case SEEK_SET: - newpos = offset; - break; - - case SEEK_CUR: - newpos = offset + handle[index].filepos; - break; - - case SEEK_END: - newpos = offset + handle[index].currentheader->length; - break; - } - if (newpos < 0) newpos = 0; - if (newpos > handle[index].currentheader->length) newpos = handle[index].currentheader->length; - handle[index].filepos = newpos; - return newpos; - } -} - -// Returns number of bytes actually read, -1 on error - -int io_read(int index, void *buffer, int length) -{ - if (!io_usedatafile) - { - return fread(buffer, 1, length, fileptr[index]); - } - else - { - int readbytes; - - if ((index < 0) || (index >= MAX_HANDLES)) return -1; - - if (!handle[index].open) return -1; - if (length + handle[index].filepos > handle[index].currentheader->length) - length = handle[index].currentheader->length - handle[index].filepos; - - if (datafilehandle) - { - fseek(datafilehandle, handle[index].currentheader->offset + handle[index].filepos, SEEK_SET); - readbytes = fread(buffer, 1, length, datafilehandle); - } - else - { - linkedseek(handle[index].currentheader->offset + handle[index].filepos); - linkedread(buffer, length); - readbytes = length; - } - handle[index].filepos += readbytes; - return readbytes; - } -} - -// Returns nothing - -void io_close(int index) -{ - if (!io_usedatafile) - { - fclose(fileptr[index]); - fileptr[index] = NULL; - } - else - { - if ((index < 0) || (index >= MAX_HANDLES)) return; - - handle[index].open = 0; - } -} - -unsigned io_read8(int index) -{ - unsigned char byte; - - io_read(index, &byte, 1); - return byte; -} - -unsigned io_readle16(int index) -{ - unsigned char bytes[2]; - - io_read(index, bytes, 2); - return (bytes[1] << 8) | bytes[0]; -} - -unsigned io_readhe16(int index) -{ - unsigned char bytes[2]; - - io_read(index, bytes, 2); - return (bytes[0] << 8) | bytes[1]; -} - -unsigned io_readle32(int index) -{ - unsigned char bytes[4]; - - io_read(index, bytes, 4); - return (bytes[3] << 24) | (bytes[2] << 16) | (bytes[1] << 8) | bytes[0]; -} - -unsigned io_readhe32(int index) -{ - unsigned char bytes[4]; - - io_read(index, bytes, 4); - return (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3]; -} - -static unsigned freadle32(FILE *file) -{ - unsigned char bytes[4]; - - fread(&bytes, 4, 1, file); - return (bytes[3] << 24) | (bytes[2] << 16) | (bytes[1] << 8) | bytes[0]; -} - -static void linkedseek(unsigned pos) -{ - datafileptr = &datafilestart[pos]; -} - -static void linkedread(void *buffer, int length) -{ - unsigned char *dest = (unsigned char *)buffer; - while (length--) - { - *dest++ = *datafileptr++; - } -} - -static unsigned linkedreadle32(void) -{ - unsigned char bytes[4]; - - linkedread(&bytes, 4); - return (bytes[3] << 24) | (bytes[2] << 16) | (bytes[1] << 8) | bytes[0]; -} - diff --git a/src/bme/bme_io.cpp b/src/bme/bme_io.cpp new file mode 100644 index 0000000..36d5797 --- /dev/null +++ b/src/bme/bme_io.cpp @@ -0,0 +1,260 @@ +// +// BME (Blasphemous Multimedia Engine) datafile IO main module +// + +#include "bme_io.h" + +#include "bme_main.h" + +#include + +#include + +#include +#include +#include +#include + +// Up to 16 simultaneous files open from the datafile +constexpr int MAX_HANDLES = 16; + +typedef struct +{ + Uint32 offset; + Sint32 length; + char name[13]; +} HEADER; + +typedef struct +{ + HEADER *currentheader; + int filepos; + bool open; +} HANDLE; + +static const char *idstring = "DAT!"; + +static bool io_datafileopen = false; +static HEADER *fileheaders; +static unsigned files; +static HANDLE handle[MAX_HANDLES]; +static FILE *datafilehandle = nullptr; +static unsigned char *datafileptr; +static unsigned char *datafilestart; + +static void linkedseek(unsigned pos); +static void linkedread(void *buffer, int length); +static unsigned linkedreadle32(void); + +int io_openlinkeddatafile(unsigned char *ptr) +{ + if (datafilehandle) std::fclose(datafilehandle); + datafilehandle = nullptr; + + datafilestart = ptr; + linkedseek(0); + + char ident[4]; + linkedread(ident, 4); + if (std::memcmp(ident, idstring, 4)) + { + bme_error = BME_WRONG_FORMAT; + return BME_ERROR; + } + + files = linkedreadle32(); + fileheaders = new (std::nothrow) HEADER[files]; + if (!fileheaders) + { + bme_error = BME_OUT_OF_MEMORY; + return BME_ERROR; + } + for (unsigned index = 0; index < files; index++) + { + fileheaders[index].offset = linkedreadle32(); + fileheaders[index].length = linkedreadle32(); + linkedread(&fileheaders[index].name, 13); + } + + for (unsigned index = 0; index < MAX_HANDLES; index++) handle[index].open = false; + io_datafileopen = true; + bme_error = BME_OK; + return BME_OK; +} + +void io_closelinkeddatafile() +{ + delete [] fileheaders; +} + +// Returns nonnegative file handle if successful, -1 on error + +int io_open(const char *name) +{ + if (!name || !io_datafileopen) return -1; + + size_t namelength = std::strlen(name); + if (namelength > 12) namelength = 12; + char namecopy[13]; + std::memcpy(namecopy, name, namelength + 1); + for (size_t index = 0; index < std::strlen(namecopy); index++) + { + namecopy[index] = toupper(namecopy[index]); + } + + for (size_t index = 0; index < MAX_HANDLES; index++) + { + if (!handle[index].open) + { + int count = files; + handle[index].currentheader = fileheaders; + + while (count) + { + if (!std::strcmp(namecopy, handle[index].currentheader->name)) + { + handle[index].open = true; + handle[index].filepos = 0; + return index; + } + count--; + handle[index].currentheader++; + } + return -1; + } + } + return -1; +} + +// Returns file position after seek or -1 on error + +int io_lseek(int index, int offset, int whence) +{ + if (!io_datafileopen) return -1; + + if ((index < 0) || (index >= MAX_HANDLES)) return -1; + + if (!handle[index].open) return -1; + int newpos; + switch(whence) + { + default: + case SEEK_SET: + newpos = offset; + break; + + case SEEK_CUR: + newpos = offset + handle[index].filepos; + break; + + case SEEK_END: + newpos = offset + handle[index].currentheader->length; + break; + } + if (newpos < 0) newpos = 0; + if (newpos > handle[index].currentheader->length) newpos = handle[index].currentheader->length; + handle[index].filepos = newpos; + return newpos; +} + +// Returns number of bytes actually read, -1 on error + +int io_read(int index, void *buffer, int length) +{ + if (!io_datafileopen) return -1; + + if ((index < 0) || (index >= MAX_HANDLES)) return -1; + + if (!handle[index].open) return -1; + if (length + handle[index].filepos > handle[index].currentheader->length) + length = handle[index].currentheader->length - handle[index].filepos; + + int readbytes; + if (datafilehandle) + { + std::fseek(datafilehandle, handle[index].currentheader->offset + handle[index].filepos, SEEK_SET); + readbytes = std::fread(buffer, 1, length, datafilehandle); + } + else + { + linkedseek(handle[index].currentheader->offset + handle[index].filepos); + linkedread(buffer, length); + readbytes = length; + } + handle[index].filepos += readbytes; + return readbytes; +} + +// Returns nothing + +void io_close(int index) +{ + if (io_datafileopen) + { + if ((index < 0) || (index >= MAX_HANDLES)) return; + + handle[index].open = false; + } +} + +unsigned io_read8(int index) +{ + unsigned char byte; + + io_read(index, &byte, 1); + return byte; +} + +unsigned io_readle16(int index) +{ + unsigned char bytes[2]; + + io_read(index, bytes, 2); + return (bytes[1] << 8) | bytes[0]; +} + +unsigned io_readhe16(int index) +{ + unsigned char bytes[2]; + + io_read(index, bytes, 2); + return (bytes[0] << 8) | bytes[1]; +} + +unsigned io_readle32(int index) +{ + unsigned char bytes[4]; + + io_read(index, bytes, 4); + return (bytes[3] << 24) | (bytes[2] << 16) | (bytes[1] << 8) | bytes[0]; +} + +unsigned io_readhe32(int index) +{ + unsigned char bytes[4]; + + io_read(index, bytes, 4); + return (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3]; +} + +static void linkedseek(unsigned pos) +{ + datafileptr = &datafilestart[pos]; +} + +static void linkedread(void *buffer, int length) +{ + unsigned char *dest = (unsigned char *)buffer; + while (length--) + { + *dest++ = *datafileptr++; + } +} + +static unsigned linkedreadle32() +{ + unsigned char bytes[4]; + + linkedread(&bytes, 4); + return (bytes[3] << 24) | (bytes[2] << 16) | (bytes[1] << 8) | bytes[0]; +} diff --git a/src/bme/bme_io.h b/src/bme/bme_io.h index 00e9467..27dd47a 100644 --- a/src/bme/bme_io.h +++ b/src/bme/bme_io.h @@ -1,14 +1,19 @@ // BME IO module header file +#ifndef BME_IO_H +#define BME_IO_H + +int io_openlinkeddatafile(unsigned char *ptr); +void io_closelinkeddatafile(); int io_open(const char *name); +void io_close(int handle); + int io_lseek(int handle, int bytes, int whence); int io_read(int handle, void *buffer, int size); -void io_close(int handle); -int io_opendatafile(char *name); -int io_openlinkeddatafile(unsigned char *ptr); -void io_setfilemode(int usedf); unsigned io_read8(int handle); unsigned io_readle16(int handle); unsigned io_readle32(int handle); unsigned io_readhe16(int handle); unsigned io_readhe32(int handle); + +#endif diff --git a/src/bme/bme_main.h b/src/bme/bme_main.h index ea440df..392e28d 100644 --- a/src/bme/bme_main.h +++ b/src/bme/bme_main.h @@ -1,5 +1,10 @@ // BME main definitions header file +#ifndef BME_MAIN_H +#define BME_MAIN_H + +#include + #define GFX_SCANLINES 1 #define GFX_DOUBLESIZE 2 #define GFX_USE1PAGE 0 @@ -8,8 +13,6 @@ #define GFX_WAITVBLANK 16 #define GFX_FULLSCREEN 32 #define GFX_WINDOW 64 -#define GFX_NOSWITCHING 128 -#define GFX_USEDIBSECTION 256 #define MOUSE_ALWAYS_VISIBLE 0 #define MOUSE_FULLSCREEN_HIDDEN 1 @@ -19,15 +22,6 @@ #define MOUSEB_RIGHT 2 #define MOUSEB_MIDDLE 4 -#define JOY_LEFT 1 -#define JOY_RIGHT 2 -#define JOY_UP 4 -#define JOY_DOWN 8 -#define JOY_FIRE1 16 -#define JOY_FIRE2 32 -#define JOY_FIRE3 64 -#define JOY_FIRE4 128 - #define LEFT 0 #define MIDDLE 128 #define RIGHT 255 @@ -128,6 +122,7 @@ #define KEY_END SDL_SCANCODE_END #define KEY_HOME SDL_SCANCODE_HOME #define KEY_INS SDL_SCANCODE_INSERT +#define KEY_CANC SDL_SCANCODE_CANCEL #define KEY_LEFT SDL_SCANCODE_LEFT #define KEY_PGDN SDL_SCANCODE_PAGEDOWN #define KEY_PGUP SDL_SCANCODE_PAGEUP @@ -159,6 +154,24 @@ #define KEY_KPEQUALS SDL_SCANCODE_KP_EQUALS #define KEY_KPPERIOD SDL_SCANCODE_KP_PERIOD +// BME function return codes + +#define BME_OK 1 +#define BME_ERROR 0 + +// BME detailed error types (in bme_error) + +#define BME_OPEN_ERROR -1 +#define BME_READ_ERROR -2 +#define BME_WRONG_FORMAT -3 +#define BME_OUT_OF_MEMORY -4 +#define BME_THREAD_ERROR -5 +#define BME_ILLEGAL_CONFIG -6 +#define BME_OUT_OF_CHANNELS -7 +#define BME_GRAPHICS_ERROR -8 +#define BME_SOUND_ERROR -9 + + typedef struct { @@ -220,3 +233,5 @@ typedef struct } LAYERHEADER; extern int bme_error; + +#endif diff --git a/src/bme/bme_mou.c b/src/bme/bme_mou.c deleted file mode 100644 index 0b67888..0000000 --- a/src/bme/bme_mou.c +++ /dev/null @@ -1,59 +0,0 @@ -// -// BME (Blasphemous Multimedia Engine) mouse module -// - -#include -#include -#include -#include - -#include "bme_main.h" -#include "bme_cfg.h" -#include "bme_win.h" -#include "bme_gfx.h" -#include "bme_io.h" -#include "bme_err.h" - -void mou_init(void); -void mou_uninit(void); -void mou_getpos(unsigned *x, unsigned *y); -void mou_getmove(int *dx, int *dy); -unsigned mou_getbuttons(void); - -void mou_init(void) -{ - win_mousebuttons = 0; -} - -void mou_uninit(void) -{ -} - -void mou_getpos(unsigned *x, unsigned *y) -{ - if (!gfx_initted) - { - *x = win_mousexpos; - *y = win_mouseypos; - } - else - { - *x = win_mousexpos * gfx_virtualxsize / gfx_windowxsize; - *y = win_mouseypos * gfx_virtualysize / gfx_windowysize; - } -} - -void mou_getmove(int *dx, int *dy) -{ - *dx = win_mousexrel; - *dy = win_mouseyrel; - win_mousexrel = 0; - win_mouseyrel = 0; - -} - -unsigned mou_getbuttons(void) -{ - return win_mousebuttons; -} - diff --git a/src/bme/bme_mou.h b/src/bme/bme_mou.h deleted file mode 100644 index f784e48..0000000 --- a/src/bme/bme_mou.h +++ /dev/null @@ -1,7 +0,0 @@ -// BME mouse functions header file - -void mou_init(void); -void mou_uninit(void); -void mou_getpos(unsigned *x, unsigned *y); -void mou_getmove(int *dx, int *dy); -unsigned mou_getbuttons(void); diff --git a/src/bme/bme_snd.c b/src/bme/bme_snd.cpp similarity index 90% rename from src/bme/bme_snd.c rename to src/bme/bme_snd.cpp index aaecbd7..29bd743 100644 --- a/src/bme/bme_snd.c +++ b/src/bme/bme_snd.cpp @@ -2,9 +2,11 @@ // BME (Blasphemous Multimedia Engine) sound main module // -#include -#include -#include +#include "bme_snd.h" + +#include "bme_main.h" +#include "bme_win.h" +#include "bme_io.h" #ifdef USE_JACK # include @@ -16,24 +18,18 @@ #endif #include -#include "bme_main.h" -#include "bme_cfg.h" -#include "bme_win.h" -#include "bme_io.h" -#include "bme_err.h" + +#include +#include +#include #ifdef USE_JACK typedef jack_default_audio_sample_t sample_t; #endif -// Prototypes -int snd_init(unsigned mixrate, unsigned mixmode, unsigned channels, int usedirectsound); -void snd_uninit(void); -void snd_setcustommixer(void (*custommixer)(Sint32 *dest, unsigned samples)); - -static int snd_initchannels(unsigned channels); -static int snd_initmixer(void); -static void snd_uninitmixer(void); +static bool snd_initchannels(unsigned channels); +static bool snd_initmixer(); +static void snd_uninitmixer(); static void snd_mixdata(Uint8 *dest, unsigned bytes); static void snd_mixchannels(Sint32 *dest, unsigned samples); static void snd_mixer_callback(void *userdata, SDL_AudioStream *stream, int additional_amount, int total_amount); @@ -49,40 +45,40 @@ static void snd_float_postprocess(Sint32 *src, float* dest, unsigned samples); static void snd_16bit_postprocess(Sint32 *src, Sint16 *dest, unsigned samples); static void snd_8bit_postprocess(Sint32 *src, Uint8 *dest, unsigned samples); -void (*snd_player)(void) = NULL; -CHANNEL *snd_channel = NULL; +void (*snd_player)() = nullptr; +CHANNEL *snd_channel = nullptr; int snd_channels = 0; -int snd_sndinitted = 0; +bool snd_sndinitted = false; int snd_bpmcount; int snd_bpmtempo = 125; unsigned snd_mixmode; unsigned snd_mixrate; -static void (*snd_custommixer)(Sint32 *dest, unsigned samples) = NULL; +static void (*snd_custommixer)(Sint32 *dest, unsigned samples) = nullptr; static unsigned snd_buffersize; static unsigned snd_framesize; static unsigned snd_previouschannels = 0xffffffff; static int snd_atexit_registered = 0; -static Sint32 *snd_clipbuffer = NULL; -SDL_AudioStream *stream = NULL; +static Sint32 *snd_clipbuffer = nullptr; +SDL_AudioStream *stream = nullptr; static SDL_AudioSpec spec; #ifdef USE_JACK -static int use_jack = 1; -static int use_jack_audio = 0; +static bool use_jack = true; +static bool use_jack_audio = false; static jack_client_t* client; static jack_port_t* output_port; #endif #ifdef USE_MIDI_INPUT -RtMidiInPtr midi_device = NULL; +RtMidiInPtr midi_device = nullptr; #endif void playtestnote(int note, int ins, int chnnum); void insertnote(int newnote); -#define VISIBLEPATTROWS 31 +#define VISIBLEPATTROWS 34 extern int einum; extern int epchn; @@ -94,26 +90,26 @@ int current_note_on = -1; #ifdef USE_JACK int snd_jack_process(jack_nframes_t nframes, void *arg) { if (use_jack_audio) { - sample_t* buffer = jack_port_get_buffer(output_port, nframes); + sample_t* buffer = (sample_t*)jack_port_get_buffer(output_port, nframes); snd_mixdata((Uint8*)buffer, sizeof(sample_t) * nframes); } return 0; } -int snd_init_jack() { +bool snd_init_jack() { jack_status_t status; client = jack_client_open("goattracker2", JackNoStartServer, &status); if (client == 0) { fprintf(stderr, "failed to create jack client\n"); - return BME_ERROR; + return false; } if (use_jack_audio) { snd_mixrate = jack_get_sample_rate(client); snd_bpmcount = 0; - snd_sndinitted = 1; + snd_sndinitted = true; // force 16 bit snd_mixmode = SIXTEENBIT; @@ -125,7 +121,7 @@ int snd_init_jack() { { bme_error = BME_OUT_OF_MEMORY; snd_uninit(); - return BME_ERROR; + return false; } } @@ -138,17 +134,17 @@ int snd_init_jack() { if (!output_port) { fprintf(stderr, "failed to register port\n"); - return BME_ERROR; + return false; } } if (jack_activate(client)) { fprintf(stderr, "failed to activate\n"); - return BME_ERROR; + return false; } bme_error = BME_OK; - return BME_OK; + return true; } #endif @@ -191,37 +187,37 @@ void snd_midi_process(double timeStamp, const unsigned char *message, size_t mes } } -int snd_init_midi() { +bool snd_init_midi() { RtMidiInPtr midi_device = rtmidi_in_create(RTMIDI_API_UNSPECIFIED, "goattracker2", 100); if (!midi_device->ok) { fprintf(stderr, "failed to activate midi: %s\n", midi_device->msg); - return BME_ERROR; + return false; } unsigned int ports = rtmidi_get_port_count(midi_device); if (!ports) { fprintf(stderr, "no available ports\n"); - return BME_ERROR; + return false; } rtmidi_open_port(midi_device, 0, "midi_in"); if (!midi_device->ok) { fprintf(stderr, "failed to open port: %s\n", midi_device->msg); - return BME_ERROR; + return false; } - rtmidi_in_set_callback(midi_device, snd_midi_process, NULL); + rtmidi_in_set_callback(midi_device, snd_midi_process, nullptr); if (!midi_device->ok) { fprintf(stderr, "failed to set midi callback: %s\n", midi_device->msg); - return BME_ERROR; + return false; } bme_error = BME_OK; - return BME_OK; + return true; } #endif -int snd_init(unsigned mixrate, unsigned mixmode, unsigned channels, int usedirectsound) +bool snd_init(unsigned mixrate, unsigned mixmode, unsigned channels, int usedirectsound) { // If user wants to re-initialize, shutdown first @@ -234,7 +230,7 @@ int snd_init(unsigned mixrate, unsigned mixmode, unsigned channels, int usedirec #ifdef USE_JACK if (use_jack) { snd_init_jack(); - if (use_jack_audio) return BME_OK; + if (use_jack_audio) return true; } #endif // Register snd_uninit as an atexit function @@ -251,7 +247,7 @@ int snd_init(unsigned mixrate, unsigned mixmode, unsigned channels, int usedirec { bme_error = BME_ILLEGAL_CONFIG; snd_uninit(); - return BME_ERROR; + return false; } spec.freq = mixrate; @@ -267,15 +263,15 @@ int snd_init(unsigned mixrate, unsigned mixmode, unsigned channels, int usedirec snd_bpmcount = 0; - stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, snd_mixer_callback, NULL); + stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, snd_mixer_callback, nullptr); if (!stream) { bme_error = BME_OPEN_ERROR; snd_uninit(); - return BME_ERROR; + return false; } - snd_sndinitted = 1; + snd_sndinitted = true; snd_mixmode = 0; snd_framesize = 1; @@ -287,8 +283,8 @@ int snd_init(unsigned mixrate, unsigned mixmode, unsigned channels, int usedirec } // (Re)allocate channels if necessary - if (snd_initchannels(spec.channels) != BME_OK) { - return BME_ERROR; + if (!snd_initchannels(spec.channels)) { + return false; } if ((SDL_AUDIO_BITSIZE(spec.format) == 16) && @@ -309,7 +305,7 @@ int snd_init(unsigned mixrate, unsigned mixmode, unsigned channels, int usedirec { bme_error = BME_SOUND_ERROR; snd_uninit(); - return BME_ERROR; + return false; } int sample_frames; @@ -324,53 +320,52 @@ int snd_init(unsigned mixrate, unsigned mixmode, unsigned channels, int usedirec { bme_error = BME_OUT_OF_MEMORY; snd_uninit(); - return BME_ERROR; + return false; } SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(stream)); bme_error = BME_OK; - return BME_OK; + return true; } -int snd_initchannels(unsigned channels) { - int c; +bool snd_initchannels(unsigned channels) { if (snd_previouschannels != channels) { CHANNEL *chptr; if (snd_channel) { - free(snd_channel); - snd_channel = NULL; + std::free(snd_channel); + snd_channel = nullptr; snd_channels = 0; } - snd_channel = malloc(channels * sizeof(CHANNEL)); + snd_channel = (CHANNEL*)std::malloc(channels * sizeof(CHANNEL)); if (!snd_channel) { bme_error = BME_OUT_OF_MEMORY; snd_uninit(); - return BME_ERROR; + return false; } chptr = &snd_channel[0]; snd_channels = channels; snd_previouschannels = channels; // Init all channels (no sound played, no sample, mastervolume 64) - for (c = snd_channels; c > 0; c--) + for (int c = snd_channels; c > 0; c--) { chptr->voicemode = VM_OFF; - chptr->smp = NULL; + chptr->smp = nullptr; chptr->mastervol = 64; chptr++; } } - return BME_OK; + return true; } -void snd_uninit(void) +void snd_uninit() { if (snd_sndinitted #ifdef USE_JACK @@ -379,11 +374,11 @@ void snd_uninit(void) ) { SDL_DestroyAudioStream(stream); - snd_sndinitted = 0; + snd_sndinitted = false; } snd_uninitmixer(); #ifdef USE_MIDI_INPUT - if (midi_device != NULL) + if (midi_device != nullptr) rtmidi_in_free(midi_device); #endif } @@ -393,7 +388,7 @@ void snd_setcustommixer(void (*custommixer)(Sint32 *dest, unsigned samples)) snd_custommixer = custommixer; } -static int snd_initmixer(void) +static bool snd_initmixer() { snd_uninitmixer(); @@ -403,18 +398,18 @@ static int snd_initmixer(void) bufSize *= 2; } - snd_clipbuffer = malloc(bufSize); - if (!snd_clipbuffer) return 0; + snd_clipbuffer = (Sint32*)std::malloc(bufSize); + if (!snd_clipbuffer) return false; - return 1; + return true; } -static void snd_uninitmixer(void) +static void snd_uninitmixer() { if (snd_clipbuffer) { - free(snd_clipbuffer); - snd_clipbuffer = NULL; + std::free(snd_clipbuffer); + snd_clipbuffer = nullptr; } } @@ -513,9 +508,8 @@ static void snd_mixdata(Uint8 *dest, unsigned bytes) static void snd_mixchannels(Sint32 *dest, unsigned samples) { CHANNEL *chptr = &snd_channel[0]; - int c; - for (c = snd_channels; c; c--) + for (int c = snd_channels; c; c--) { snd_mixchannel(chptr, dest, samples); chptr++; diff --git a/src/bme/bme_snd.h b/src/bme/bme_snd.h index b1c7826..2fafb34 100644 --- a/src/bme/bme_snd.h +++ b/src/bme/bme_snd.h @@ -1,19 +1,18 @@ // Sound functions header file -int snd_init(unsigned mixrate, unsigned mixmode, unsigned channels, int usedirectsound); -void snd_uninit(void); +#ifndef BME_SND_H +#define BME_SND_H + +#include "bme_main.h" + +#include + +bool snd_init(unsigned mixrate, unsigned mixmode, unsigned channels, int usedirectsound); +void snd_uninit(); void snd_setcustommixer(void (*custommixer)(Sint32 *dest, unsigned samples)); -void snd_preventdistortion(unsigned channels); -void snd_setmastervolume(unsigned chnum, unsigned char mastervol); -void snd_setmusicmastervolume(unsigned musicchannels, unsigned char mastervol); -void snd_setsfxmastervolume(unsigned musicchannels, unsigned char mastervol); -extern void (*snd_player)(void); -extern CHANNEL *snd_channel; -extern int snd_sndinitted; +extern void (*snd_player)(); extern int snd_bpmtempo; -extern int snd_bpmcount; -extern int snd_channels; -extern int snd_buffers; -extern unsigned snd_mixmode; extern unsigned snd_mixrate; + +#endif diff --git a/src/bme/bme_win.c b/src/bme/bme_win.cpp similarity index 78% rename from src/bme/bme_win.c rename to src/bme/bme_win.cpp index 3cf6ff4..bb191ac 100644 --- a/src/bme/bme_win.c +++ b/src/bme/bme_win.cpp @@ -2,40 +2,29 @@ // BME (Blasphemous Multimedia Engine) windows & timing module // -#include -#include -#include +#include "bme_win.h" -#include #include "bme_main.h" #include "bme_gfx.h" -#include "bme_mou.h" #include "bme_io.h" -#include "bme_err.h" -#include "bme_cfg.h" -SDL_Joystick *joy[MAX_JOYSTICKS] = {NULL}; -Sint16 joyx[MAX_JOYSTICKS]; -Sint16 joyy[MAX_JOYSTICKS]; -Uint32 joybuttons[MAX_JOYSTICKS]; -SDL_Window *win_window = NULL; +#include + +#include +#include +#include -// Prototypes +SDL_Window *win_window = nullptr; -int win_openwindow(unsigned xsize, unsigned ysize, const char *appname, char *icon); -void win_closewindow(void); -void win_checkmessages(void); -int win_getspeed(int framerate); -void win_setmousemode(int mode); +void win_checkmessages(); // Global variables int win_fullscreen = 0; // By default windowed -int win_windowinitted = 0; +bool win_windowinitted = false; int win_quitted = 0; unsigned char win_keytable[SDL_SCANCODE_COUNT] = {0}; unsigned char win_asciikey = 0; -unsigned win_virtualkey = 0; unsigned win_mousexpos = 0; unsigned win_mouseypos = 0; unsigned win_mousexrel = 0; @@ -45,10 +34,12 @@ int win_mousemode = MOUSE_FULLSCREEN_HIDDEN; float win_mouseywheel = 0.f; unsigned char win_keystate[SDL_SCANCODE_COUNT] = {0}; +int bme_error = BME_OK; + // Static variables -static int win_lasttime = 0; -static int win_currenttime = 0; +static Uint64 win_lasttime = 0; +static Uint64 win_currenttime = 0; static int win_framecounter = 0; static int win_activateclick = 0; @@ -63,8 +54,8 @@ int win_openwindow(unsigned xsize, unsigned ysize, const char *appname, char *ic { return BME_ERROR; } - atexit(SDL_Quit); - win_windowinitted = 1; + std::atexit(SDL_Quit); + win_windowinitted = true; } //SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); @@ -94,7 +85,7 @@ int win_getspeed(int framerate) int frametime = 10000 / framerate; int frames = 0; - while (!frames) + while (frames == 0) { win_checkmessages(); @@ -118,7 +109,7 @@ int win_getspeed(int framerate) // It is recommended to be called in any long loop where those two functions // are not called. -void win_checkmessages(void) +void win_checkmessages() { SDL_Event event; unsigned keynum; @@ -132,27 +123,6 @@ void win_checkmessages(void) { switch (event.type) { - case SDL_EVENT_JOYSTICK_BUTTON_DOWN: - joybuttons[event.jbutton.which] |= 1 << event.jbutton.button; - break; - - case SDL_EVENT_JOYSTICK_BUTTON_UP: - joybuttons[event.jbutton.which] &= ~(1 << event.jbutton.button); - break; - - case SDL_EVENT_JOYSTICK_AXIS_MOTION: - switch (event.jaxis.axis) - { - case 0: - joyx[event.jaxis.which] = event.jaxis.value; - break; - - case 1: - joyy[event.jaxis.which] = event.jaxis.value; - break; - } - break; - case SDL_EVENT_MOUSE_MOTION: win_mousexpos = event.motion.x; win_mouseypos = event.motion.y; @@ -207,7 +177,6 @@ void win_checkmessages(void) break; case SDL_EVENT_KEY_DOWN: - win_virtualkey = event.key.key; keynum = event.key.scancode; if (keynum < SDL_SCANCODE_COUNT) { @@ -234,7 +203,7 @@ void win_checkmessages(void) case SDL_EVENT_WINDOW_RESIZED: //case SDL_VIDEOEXPOSE: - gfx_redraw = 1; + gfx_redraw = true; break; } } @@ -267,3 +236,26 @@ void win_setmousemode(int mode) } } +void mou_init() +{ + win_mousebuttons = 0; +} + +void mou_getpos(unsigned *x, unsigned *y) +{ + if (!gfx_initted) + { + *x = win_mousexpos; + *y = win_mouseypos; + } + else + { + *x = win_mousexpos * gfx_virtualxsize / gfx_windowxsize; + *y = win_mouseypos * gfx_virtualysize / gfx_windowysize; + } +} + +unsigned mou_getbuttons(void) +{ + return win_mousebuttons; +} diff --git a/src/bme/bme_win.h b/src/bme/bme_win.h index c1b59d1..57c36de 100644 --- a/src/bme/bme_win.h +++ b/src/bme/bme_win.h @@ -1,28 +1,26 @@ // BME windows, input & timing module header file +#ifndef BME_WIN_H +#define BME_WIN_H + +#include + int win_openwindow(unsigned xsize, unsigned ysize, const char *appname, char *icon); void win_closewindow(void); -void win_checkmessages(void); int win_getspeed(int framerate); void win_setmousemode(int mode); -extern int win_windowinitted; +void mou_init(); +void mou_getpos(unsigned *x, unsigned *y); +unsigned mou_getbuttons(); + extern int 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 unsigned win_virtualkey; -extern unsigned win_mousexpos; -extern unsigned win_mouseypos; -extern unsigned win_mousexrel; -extern unsigned win_mouseyrel; -extern unsigned win_mousebuttons; extern int win_mousemode; extern float win_mouseywheel; -extern SDL_Joystick *joy[MAX_JOYSTICKS]; -extern Sint16 joyx[MAX_JOYSTICKS]; -extern Sint16 joyy[MAX_JOYSTICKS]; -extern Uint32 joybuttons[MAX_JOYSTICKS]; extern SDL_Window *win_window; +#endif diff --git a/src/bme/datafile.c b/src/bme/datafile.c deleted file mode 100644 index 0ebf9ed..0000000 --- a/src/bme/datafile.c +++ /dev/null @@ -1,166 +0,0 @@ -// -// Datafile creator -// - -#include -#include -#include -#include -#include - -#include "bme_end.h" - -#define MAXFILES 16384 -#define MAXFILENAME 64 - -typedef struct -{ - uint32_t offset; - uint32_t length; - char name[13]; -} HEADER; - -static HEADER header[MAXFILES]; -static char fullname[MAXFILES][MAXFILENAME]; -static int files; - -int main(int argc, char **argv); -int addfile(HEADER *header, FILE *dest, char *name); -void fwrite8(FILE *file, unsigned data); -void fwritele16(FILE *file, unsigned data); -void fwritele32(FILE *file, unsigned data); - -int main(int argc, char **argv) -{ - FILE *datafile, *listfile; - int c; - - if (argc < 3) - { - printf("Usage: DATAFILE \n\n" - "The purpose of this program is to gather many files into one datafile, like\n" - "usually seen in games. The files can be read by BME's IO functions after\n" - "opening the datafile first with io_opendatafile(). The filelistfile must\n" - "contain name (use only 8+3 chars) of each file on its own row, for example:\n\n" - "editor.spr\n" - "fonts.spr\n"); - return 0; - } - listfile = fopen(argv[2], "rt"); - if (!listfile) - { - printf("ERROR: Couldn't open filelist.\n"); - return 1; - } - datafile = fopen(argv[1], "wb"); - if (!datafile) - { - printf("ERROR: Couldn't create datafile.\n"); - fclose(listfile); - return 1; - } - memset(&header[0], 0, MAXFILES * sizeof(HEADER)); - // Get names from list - for (;;) - { - char searchname[64]; - int d; - FILE *test; - - if (fscanf(listfile, "%63s", searchname) == EOF) break; - test = fopen(searchname, "rb"); - if (test) - { - fclose(test); - - strcpy(fullname[files], searchname); - - for (c = strlen(fullname[files]) - 1; c >= 0; c--) - { - if (fullname[files][c] == '\\') - { - c++; - break; - } - } - if (c < 0) c = 0; - memset(header[files].name, 0, 13); - d = 0; - while (fullname[files][c]) - { - header[files].name[d] = toupper(fullname[files][c]); - c++; - d++; - } - files++; - if (files == MAXFILES) break; - } - if (files == MAXFILES) break; - } - - fclose(listfile); - // Write datafile header - fwrite("DAT!", 4, 1, datafile); - fwritele32(datafile, files); - // Write incomplete fileheaders - for (c = 0; c < files; c++) - { - fwritele32(datafile, header[c].offset); - fwritele32(datafile, header[c].length); - fwrite(header[c].name, 13, 1, datafile); - } - // Process each file - for (c = 0; c < files; c++) - { - printf("Adding %s...\n", header[c].name); - if (!addfile(&header[c], datafile, fullname[c])) - { - printf("Terminating & deleting datafile...\n"); - fclose(datafile); - remove(argv[1]); - return 1; - } - } - // Seek back to start & write correct headers - fseek(datafile, sizeof files + 4, SEEK_SET); - for (c = 0; c < files; c++) - { - fwritele32(datafile, header[c].offset); - fwritele32(datafile, header[c].length); - fwrite(header[c].name, 13, 1, datafile); - } - fclose(datafile); - printf("Everything OK!\n"); - return 0; -} - -int addfile(HEADER *header, FILE *dest, char *name) -{ - FILE *src; - unsigned char *originalbuf; - - src = fopen(name, "rb"); - if (!src) - { - printf("ERROR: Couldn't open file %s\n", name); - return 0; - } - header->offset = ftell(dest); - fseek(src, 0, SEEK_END); - header->length = ftell(src); - fseek(src, 0, SEEK_SET); - originalbuf = malloc(header->length); - if (!originalbuf) - { - printf("ERROR: No memory to load file!\n"); - fclose(src); - return 0; - } - printf("* Loading file\n"); - fread(originalbuf, header->length, 1, src); - fclose(src); - printf("* Writing file (size was %d)\n", header->length); - fwrite(originalbuf, header->length, 1, dest); - free(originalbuf); - return 1; -} diff --git a/src/gcommon.h b/src/common.h similarity index 97% rename from src/gcommon.h rename to src/common.h index 8b26566..ca4429b 100644 --- a/src/gcommon.h +++ b/src/common.h @@ -1,5 +1,5 @@ -#ifndef GCOMMON_H -#define GCOMMON_H +#ifndef COMMON_H +#define COMMON_H #define CMD_DONOTHING 0 #define CMD_PORTAUP 1 diff --git a/src/gconsole.c b/src/console.cpp similarity index 66% rename from src/gconsole.c rename to src/console.cpp index e7c6dad..9c66330 100644 --- a/src/gconsole.c +++ b/src/console.cpp @@ -1,25 +1,47 @@ /* - * ============================================================================= - * "console" output routines - * ============================================================================= + * LoadTracker + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#define GCONSOLE_C +// ============================================================================= +// "console" output routines +// ============================================================================= + +#define CONSOLE_C #include "loadtrk.h" +#include "bme_main.h" +#include "bme_win.h" +#include "bme_gfx.h" +#include "bme_io.h" + +#include -#include -#include -#include +#include +#include +#include -int gfxinitted = 0; -unsigned *scrbuffer = NULL; -unsigned *prevscrbuffer = NULL; -unsigned char *chardata = NULL; +bool gfxinitted = false; +unsigned *scrbuffer = nullptr; +unsigned *prevscrbuffer = nullptr; +unsigned char *chardata = nullptr; int key = 0; int rawkey = 0; -int shiftpressed = 0; -int altpressed = 0; +bool shiftpressed = false; +bool altpressed = false; int cursorflashdelay = 0; int mouseb = 0; int prevmouseb = 0; @@ -33,8 +55,8 @@ int mouseheld = 0; int region[MAX_ROWS]; int fontwidth = 8; int fontheight = 14; -int mousesizex = 11; -int mousesizey = 20; +int mousesizex = 19; +int mousesizey = 24; POSITIONS dpos = { @@ -58,8 +80,8 @@ POSITIONS dpos = 0 // statusTopY }; -void loadexternalpalette(void); -void initicon(void); +void loadexternalpalette(); +void initicon(); static inline void setcharcolor(unsigned *dptr, short ch, short color) { @@ -71,15 +93,15 @@ static inline void setcolor(unsigned *dptr, short color) *dptr = (*dptr & 0xffff) | (color << 16) | (colors.CBKGND << 20); } -int initscreen(void) +bool initscreen() { - int handle; + if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) + return false; + unsigned xsize = MAX_COLUMNS * 8; unsigned ysize = MAX_ROWS * 16; - if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) - return 0; - win_openwindow(xsize, ysize, "LoadTracker", NULL); + win_openwindow(xsize, ysize, "LoadTracker", nullptr); win_setmousemode(MOUSE_ALWAYS_HIDDEN); initicon(); @@ -87,56 +109,56 @@ int initscreen(void) { win_fullscreen = 0; if (!gfx_init(MAX_COLUMNS * fontwidth, MAX_ROWS * fontheight, 60, 0)) - return 0; + return false; } - scrbuffer = (unsigned*)malloc(MAX_COLUMNS * MAX_ROWS * sizeof(unsigned)); - prevscrbuffer = (unsigned*)malloc(MAX_COLUMNS * MAX_ROWS * sizeof(unsigned)); - if ((!scrbuffer) || (!prevscrbuffer)) return 0; + scrbuffer = new unsigned[MAX_COLUMNS * MAX_ROWS]; + prevscrbuffer = new unsigned[MAX_COLUMNS * MAX_ROWS]; - memset(region, 0, sizeof region); + std::memset(region, 0, sizeof region); - chardata = (unsigned char*)malloc(4096); - if (!chardata) return 0; - handle = io_open("chargen.bin"); - if (handle == -1) return 0; + chardata = new unsigned char[4096]; + int handle = io_open("chargen.bin"); + if (handle == -1) return false; io_read(handle, &chardata[0], 4096); io_close(handle); - gfx_loadpalette("palette.bin"); + if (!gfx_loadpalette("palette.bin")) + return false; loadexternalpalette(); gfx_setpalette(); - gfx_loadsprites(0, "cursor.bin"); + if (!gfx_loadcursor("cursor.png")) + return false; - gfxinitted = 1; + gfxinitted = true; clearscreen(); - atexit(closescreen); - return 1; + std::atexit(closescreen); + return true; } -void loadexternalpalette(void) +void loadexternalpalette() { - FILE *ext_f; - if ((ext_f = fopen("custom.pal", "rt"))) + FILE *ext_f = std::fopen("custom.pal", "rt"); + if (ext_f) { - int p = 0; char ln[100]; - strcpy(ln, ""); - fgets(ln, sizeof(ln), ext_f); + std::strcpy(ln, ""); + std::fgets(ln, sizeof(ln), ext_f); - if (strncmp("JASC-PAL", ln, 8) == 0) + if (std::strncmp("JASC-PAL", ln, 8) == 0) { int colors; - fgets(ln, sizeof(ln), ext_f); - fgets(ln, sizeof(ln), ext_f); - if (sscanf(ln, "%d", &colors) == 1 && colors == 256) + std::fgets(ln, sizeof(ln), ext_f); + std::fgets(ln, sizeof(ln), ext_f); + if (std::sscanf(ln, "%d", &colors) == 1 && colors == 256) { + int p = 0; while (!feof(ext_f)) { int r, g, b; - if (!fgets(ln, sizeof(ln), ext_f)) break; - if (sscanf(ln, "%d %d %d", &r, &g, &b) == 3) + if (!std::fgets(ln, sizeof(ln), ext_f)) break; + if (std::sscanf(ln, "%d %d %d", &r, &g, &b) == 3) { // JASC palette is 8-bit and goat palette is 6-bit gfx_palette[p++] = r / 4; @@ -150,63 +172,60 @@ void loadexternalpalette(void) } } - fclose(ext_f); + std::fclose(ext_f); } } -void initicon(void) +void initicon() { int handle = io_open("loadtrk.bmp"); if (handle != -1) { - SDL_IOStream *rw; - SDL_Surface *icon; - char *iconbuffer; - int size; - - size = io_lseek(handle, 0, SEEK_END); + int size = io_lseek(handle, 0, SEEK_END); io_lseek(handle, 0, SEEK_SET); - iconbuffer = (char*)malloc(size); + char *iconbuffer = new (std::nothrow) char[size]; if (iconbuffer) { io_read(handle, iconbuffer, size); io_close(handle); - rw = SDL_IOFromMem(iconbuffer, size); - icon = SDL_LoadBMP_IO(rw, 0); + SDL_IOStream *rw = SDL_IOFromMem(iconbuffer, size); + SDL_Surface *icon = SDL_LoadBMP_IO(rw, true); SDL_SetWindowIcon(win_window, icon); - free(iconbuffer); + SDL_DestroySurface(icon); + delete [] iconbuffer; } } } -void closescreen(void) +void closescreen() { if (scrbuffer) { - free(scrbuffer); - scrbuffer = NULL; + delete [] scrbuffer; + scrbuffer = nullptr; } if (prevscrbuffer) { - free(prevscrbuffer); - prevscrbuffer = NULL; + delete [] prevscrbuffer; + prevscrbuffer = nullptr; } if (chardata) { - free(chardata); - chardata = NULL; + delete [] chardata; + chardata = nullptr; } - gfxinitted = 0; + gfx_freecursor(); + + gfxinitted = false; } -void clearscreen(void) +void clearscreen() { - int c; - unsigned *dptr = scrbuffer; - if (!gfxinitted) return; - for (c = 0; c < MAX_ROWS * MAX_COLUMNS; c++) + unsigned *dptr = scrbuffer; + + for (int c = 0; c < MAX_ROWS * MAX_COLUMNS; c++) { setcharcolor(dptr, 0x20, 0x7); dptr++; @@ -215,11 +234,11 @@ void clearscreen(void) void printtext(int x, int y, int color, const char *text) { + if (!gfxinitted) return; + if ((y < 0) || (y >= MAX_ROWS)) return; + unsigned *dptr = scrbuffer + (x + y * MAX_COLUMNS); - if (!gfxinitted) return; - if (y < 0) return; - if (y >= MAX_ROWS) return; while (*text) { setcharcolor(dptr, *text, color); @@ -230,14 +249,14 @@ void printtext(int x, int y, int color, const char *text) void printtextc(int y, int color, const char *text) { - int x = (MAX_COLUMNS - strlen(text)) / 2; + int x = (MAX_COLUMNS - std::strlen(text)) / 2; printtext(x, y, color, text); } void printtextcp(int cp, int y, int color, const char *text) { - int x = cp - (strlen(text) / 2); + int x = cp - (std::strlen(text) / 2); printtext(x, y, color, text); } @@ -245,11 +264,11 @@ void printtextcp(int cp, int y, int color, const char *text) void printblank(int x, int y, int length) { + if (!gfxinitted) return; + if ((y < 0) | (y >= MAX_ROWS)) return; + unsigned *dptr = scrbuffer + (x + y * MAX_COLUMNS); - if (!gfxinitted) return; - if (y < 0) return; - if (y >= MAX_ROWS) return; while (length--) { setcharcolor(dptr, 0x20, 0x7); @@ -259,11 +278,11 @@ void printblank(int x, int y, int length) void printblankc(int x, int y, int color, int length) { + if (!gfxinitted) return; + if ((y < 0) || (y >= MAX_ROWS)) return; + unsigned *dptr = scrbuffer + (x + y * MAX_COLUMNS); - if (!gfxinitted) return; - if (y < 0) return; - if (y >= MAX_ROWS) return; while (length--) { setcharcolor(dptr, 0x20, color); @@ -273,19 +292,15 @@ void printblankc(int x, int y, int color, int length) void drawbox(int x, int y, int color, int sx, int sy) { - unsigned *dptr; - unsigned *dptr2; - int counter; - if (!gfxinitted) return; if (y < 0) return; if (y >= MAX_ROWS) return; if (y+sy > MAX_ROWS) return; if ((!sx) || (!sy)) return; - dptr = scrbuffer + (x + y * MAX_COLUMNS); - dptr2 = scrbuffer + ((x+sx-1) + y * MAX_COLUMNS); - counter = sy; + unsigned *dptr = scrbuffer + (x + y * MAX_COLUMNS); + unsigned *dptr2 = scrbuffer + ((x+sx-1) + y * MAX_COLUMNS); + int counter = sy; while (counter--) { @@ -322,11 +337,11 @@ void drawbox(int x, int y, int color, int sx, int sy) void printbg(int x, int y, int color, int length) { + if (!gfxinitted) return; + if ((y < 0) || (y >= MAX_ROWS)) return; + unsigned *dptr = scrbuffer + (x + y * MAX_COLUMNS); - if (!gfxinitted) return; - if (y < 0) return; - if (y >= MAX_ROWS) return; while (length--) { setcolor(dptr, 15 | (color << 4)); @@ -334,13 +349,8 @@ void printbg(int x, int y, int color, int length) } } -void fliptoscreen(void) +void fliptoscreen() { - unsigned *sptr = scrbuffer; - unsigned *cmpptr = prevscrbuffer; - int x,y; - int regionschanged = 0; - if (!gfxinitted) return; // Mark previous mousecursor area changed if mouse moved @@ -354,9 +364,9 @@ void fliptoscreen(void) if (ey >= MAX_ROWS) ey = MAX_ROWS - 1; if (ex >= MAX_COLUMNS) ex = MAX_COLUMNS - 1; - for (y = sy; y <= ey; y++) + for (int y = sy; y <= ey; y++) { - for (x = sx; x <= ex; x++) + for (int x = sx; x <= ex; x++) prevscrbuffer[y*MAX_COLUMNS+x] = 0xffffffff; } } @@ -364,23 +374,28 @@ void fliptoscreen(void) // If redraw requested, mark whole screen changed if (gfx_redraw) { - gfx_redraw = 0; - memset(prevscrbuffer, 0xff, MAX_COLUMNS*MAX_ROWS*sizeof(unsigned)); + gfx_redraw = false; + std::memset(prevscrbuffer, 0xff, MAX_COLUMNS*MAX_ROWS*sizeof(unsigned)); } if (!gfx_lock()) return; + unsigned *sptr = scrbuffer; + unsigned *cmpptr = prevscrbuffer; + + bool regionschanged = false; + // Now redraw text on changed areas - for (y = 0; y < MAX_ROWS; y++) + for (int y = 0; y < MAX_ROWS; y++) { - for (x = 0; x < MAX_COLUMNS; x++) + for (int x = 0; x < MAX_COLUMNS; x++) { // Check if char changed if (*sptr != *cmpptr) { *cmpptr = *sptr; region[y] = 1; - regionschanged = 1; + regionschanged = true; { unsigned char *chptr = &chardata[(*sptr & 0xffff)*16]; @@ -420,6 +435,7 @@ void fliptoscreen(void) } } + gfx_unlock(); // Redraw mouse if text was redrawn if (regionschanged) @@ -428,8 +444,8 @@ void fliptoscreen(void) int ey = (mousepixely + mousesizey - 1) / fontheight; if (ey >= MAX_ROWS) ey = MAX_ROWS - 1; - gfx_drawsprite(mousepixelx, mousepixely, 0x1); - for (y = sy; y <= ey; y++) + gfx_drawcursor(mousepixelx, mousepixely); + for (int y = sy; y <= ey; y++) region[y] = 1; } @@ -438,13 +454,30 @@ void fliptoscreen(void) oldmousepixely = mousepixely; // Redraw changed screen regions - gfx_unlock(); gfx_flip(); } -void getkey(void) +void editstring(char *buffer, int maxlength) +{ + int len = std::strlen(buffer); + + if ((key >= 32) && (key < 256)) + { + if (len < maxlength-1) + { + buffer[len] = key; + buffer[len+1] = 0; + } + } + + if ((rawkey == KEY_BACKSPACE) && (len > 0)) + { + buffer[len-1] = 0; + } +} + +void getkey() { - int c; win_asciikey = 0; cursorflashdelay += win_getspeed(50); @@ -460,7 +493,7 @@ void getkey(void) key = win_asciikey; rawkey = 0; - for (c = 0; c < SDL_SCANCODE_COUNT; c++) + for (int c = 0; c < SDL_SCANCODE_COUNT; c++) { if (win_keytable[c]) { @@ -474,19 +507,11 @@ void getkey(void) } } - shiftpressed = 0; - if ((win_keystate[SDL_SCANCODE_LSHIFT])||(win_keystate[SDL_SCANCODE_RSHIFT])|| - (win_keystate[SDL_SCANCODE_LCTRL])||(win_keystate[SDL_SCANCODE_RCTRL])) - { - shiftpressed = 1; - } + shiftpressed = (win_keystate[SDL_SCANCODE_LSHIFT])||(win_keystate[SDL_SCANCODE_RSHIFT]) + || (win_keystate[SDL_SCANCODE_LCTRL])||(win_keystate[SDL_SCANCODE_RCTRL]); - altpressed = 0; - if ((win_keystate[SDL_SCANCODE_LALT])|| - (win_keystate[SDL_SCANCODE_RALT])) - { - altpressed = 1; - } + altpressed = (win_keystate[SDL_SCANCODE_LALT]) + || (win_keystate[SDL_SCANCODE_RALT]); if (rawkey == SDL_SCANCODE_KP_ENTER) { @@ -506,7 +531,7 @@ void getkey(void) if (rawkey == SDL_SCANCODE_KP_9) key = '9'; } -void initDisplayPositions(void) +void initDisplayPositions() { if (numsids == 1) { diff --git a/src/console.h b/src/console.h new file mode 100644 index 0000000..813a3f1 --- /dev/null +++ b/src/console.h @@ -0,0 +1,72 @@ +/* + * LoadTracker + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef CONSOLE_H +#define CONSOLE_H + +#define MAX_COLUMNS 128 +#define MAX_ROWS 40 + +typedef struct +{ + int channelsX; + int channelsY; + int instrumentsX; + int instrumentsY; + int loadboxX; + int loadboxY; + int octaveX; + int octaveY; + int orderlistX; + int orderlistY; + int patternsX; + int patternsY; + int statusBottomX; + int statusBottomY; + int statusTopEndX; + int statusTopFvX; + int statusTopX; + int statusTopY; +} POSITIONS; + + +bool initscreen(); +void closescreen(); +void clearscreen(); +void fliptoscreen(); +void printtext(int x, int y, int color, const char *text); +void printtextc(int y, int color, const char *text); +void printtextcp(int cp, int y, int color, const char *text); +void printblank(int x, int y, int length); +void printblankc(int x, int y, int color, int length); +void drawbox(int x, int y, int color, int sx, int sy); +void printbg(int x, int y, int color, int length); +void editstring(char *buffer, int maxlength); +void getkey(); +void initDisplayPositions(); + +#ifndef CONSOLE_C +extern bool shiftpressed, altpressed; +extern int key, rawkey, cursorflashdelay; +extern int mouseb, prevmouseb; +extern int mouseheld; +extern int mousex, mousey; +extern POSITIONS dpos; +#endif + +#endif diff --git a/src/cursor.bin b/src/cursor.bin deleted file mode 100644 index 94dfbcb..0000000 Binary files a/src/cursor.bin and /dev/null differ diff --git a/src/cursor.lbm b/src/cursor.lbm deleted file mode 100644 index 75bca50..0000000 Binary files a/src/cursor.lbm and /dev/null differ diff --git a/src/cursor.png b/src/cursor.png new file mode 100644 index 0000000..a85e471 Binary files /dev/null and b/src/cursor.png differ diff --git a/src/gdisplay.c b/src/display.cpp similarity index 75% rename from src/gdisplay.c rename to src/display.cpp index dae8867..0d3ef01 100644 --- a/src/gdisplay.c +++ b/src/display.cpp @@ -1,14 +1,52 @@ /* - * ============================================================================= - * screen display routines - * ============================================================================= + * LoadTracker + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#define GDISPLAY_C +// ============================================================================= +// screen display routines +// ============================================================================= -#include "loadtrk.h" +#define DISPLAY_C -char *notename[] = +#include "loadtrk.h" +#include "bme_main.h" +#include "bme_snd.h" + +#include +#include + +#define CBLACK 0x0 +#define CDBLUE 0x1 +#define CDGREEN 0x2 +#define CDGREY 0x3 +#define CDRED 0x4 +#define CDBROWN 0x5 +#define CLBROWN 0x6 +#define CLGREY 0x7 +#define CGREY 0x8 +#define CLBLUE 0x9 +#define CLGREEN 0xA +#define CCYAN 0xB +#define CLRED 0xC +#define CPURPLE 0xD +#define CYELLOW 0xE +#define CWHITE 0xF + +const char *notename[] = {"C-0", "C#0", "D-0", "D#0", "E-0", "F-0", "F#0", "G-0", "G#0", "A-0", "A#0", "B-0", "C-1", "C#1", "D-1", "D#1", "E-1", "F-1", "F#1", "G-1", "G#1", "A-1", "A#1", "B-1", "C-2", "C#2", "D-2", "D#2", "E-2", "F-2", "F#2", "G-2", "G#2", "A-2", "A#2", "B-2", @@ -24,7 +62,7 @@ int timemin = 0; int timesec = 0; unsigned timeframe = 0; -void initcolorscheme(int dark) +void initcolorscheme(bool dark) { colors.CBKGND = dark ? CBLACK : CDBLUE; @@ -41,14 +79,14 @@ void initcolorscheme(int dark) colors.CHEADER = colors.CHDRFG|(colors.CHDRBG<<4); } -void printmainscreen(void) +void printmainscreen() { clearscreen(); printstatus(); fliptoscreen(); } -void displayupdate(void) +void displayupdate() { if (cursorflashdelay >= 6) { @@ -60,9 +98,8 @@ void displayupdate(void) fliptoscreen(); } -void printstatus(void) +void printstatus() { - int c, d, color, color2; int cc = cursorcolortable[cursorflash]; int visibleOrderlist = 14; int maxChns = MAX_CHN; @@ -71,9 +108,9 @@ void printstatus(void) maxChns = 3; visibleOrderlist = VISIBLEORDERLIST; } - menu = 0; + menu = false; - if ((mouseb > MOUSEB_LEFT) && (mousey <= 1) && (!eamode)) menu = 1; + if ((mouseb > MOUSEB_LEFT) && (mousey <= 1) && (!eamode)) menu = true; printblankc(0, 0, colors.CHEADER, MAX_COLUMNS); @@ -85,10 +122,10 @@ void printstatus(void) printtext(dpos.statusTopX+11, dpos.statusTopY, CLBROWN|(colors.CHDRBG<<4), "\"\"\""); printtext(dpos.statusTopX+14, dpos.statusTopY, CDRED|(colors.CHDRBG<<4), "\"\"\" "); - if (!strlen(loadedsongfilename)) - sprintf(textbuffer, "%s", programname); + if (!std::strlen(loadedsongfilename)) + std::sprintf(textbuffer, "%s", programname); else - sprintf(textbuffer, "%s - %s", programname, loadedsongfilename); + std::sprintf(textbuffer, "%s - %s", programname, loadedsongfilename); textbuffer[49] = 0; printtext(dpos.statusTopX+19, dpos.statusTopY, colors.CHEADER, textbuffer); @@ -114,20 +151,20 @@ void printstatus(void) else printtext(dpos.statusTopFvX+14, dpos.statusTopY, colors.CHEADER, "8580"); - sprintf(textbuffer, "HR:%04X", adparam); + std::sprintf(textbuffer, "HR:%04X", adparam); printtext(dpos.statusTopFvX+19, dpos.statusTopY, colors.CHEADER, textbuffer); if (eamode && !ebmode) printbg(dpos.statusTopFvX+22+eacolumn, dpos.statusTopY, cc, 1); if (multiplier) { - sprintf(textbuffer, "%2dX", multiplier); + std::sprintf(textbuffer, "%2dX", multiplier); printtext(dpos.statusTopFvX+27, dpos.statusTopY, colors.CHEADER, textbuffer); } else printtext(dpos.statusTopFvX+27, dpos.statusTopY, colors.CHEADER, "25Hz"); if (multiplier == 1) { - sprintf(textbuffer, "%03dBPM", snd_bpmtempo); + std::sprintf(textbuffer, "%03dBPM", snd_bpmtempo); printtext(dpos.statusTopFvX+31, dpos.statusTopY, colors.CHEADER, textbuffer); if (eamode && ebmode) printbg(dpos.statusTopFvX+31+eacolumn, dpos.statusTopY, cc, 1); @@ -142,7 +179,7 @@ void printstatus(void) if ((followplay) && (isplaying())) { - for (c = 0; c < maxChns; c++) + for (int c = 0; c < maxChns; c++) { int currentSonglen = 0; if (numsids == 1) @@ -184,21 +221,22 @@ void printstatus(void) } } - for (c = 0; c < maxChns; c++) + for (int c = 0; c < maxChns; c++) { - sprintf(textbuffer, "CH."); + std::sprintf(textbuffer, "CH."); printtext(dpos.patternsX+c*13, dpos.patternsY, colors.CTITLE, textbuffer); - sprintf(textbuffer, "%d", c+1); + std::sprintf(textbuffer, "%d", c+1); printtext(dpos.patternsX+3+c*13, dpos.patternsY, colors.CTITLE, textbuffer); - sprintf(textbuffer, "PATT."); + std::sprintf(textbuffer, "PATT."); printtext(dpos.patternsX+5+c*13, dpos.patternsY, colors.CTITLE, textbuffer); - sprintf(textbuffer, "%02X", epnum[c]); + std::sprintf(textbuffer, "%02X", epnum[c]); printtext(dpos.patternsX+10+c*13, dpos.patternsY, colors.CTITLE, textbuffer); - for (d = 0; d < VISIBLEPATTROWS; d++) + for (int d = 0; d < VISIBLEPATTROWS; d++) { int p = epview+d; - color = colors.CNORMAL; + int color = colors.CNORMAL; + int color2; if ((epnum[c] == chn[c].pattnum) && (isplaying())) { int chnrow = chn[c].pattptr / 4; @@ -210,28 +248,28 @@ void printstatus(void) if (p == eppos) color = colors.CEDIT; if ((p < 0) || (p > pattlen[epnum[c]])) { - sprintf(textbuffer, " "); + std::sprintf(textbuffer, " "); } else { if (!(patterndispmode & 1)) { if (p < 100) - sprintf(textbuffer, " %02d", p); + std::sprintf(textbuffer, " %02d", p); else - sprintf(textbuffer, "%03d", p); + std::sprintf(textbuffer, "%03d", p); } else - sprintf(textbuffer, " %02X", p); + std::sprintf(textbuffer, " %02X", p); if (pattern[epnum[c]][p*4] == ENDPATT) { - sprintf(&textbuffer[3], " PATT. END"); + std::sprintf(&textbuffer[3], " PATT. END"); if (color == colors.CNORMAL) color = colors.CCOMMAND; } else { - sprintf(&textbuffer[3], " %s %02X%01X%02X", + std::sprintf(&textbuffer[3], " %s %02X%01X%02X", notename[pattern[epnum[c]][p*4]-FIRSTNOTE], pattern[epnum[c]][p*4+1], pattern[epnum[c]][p*4+2], @@ -239,9 +277,9 @@ void printstatus(void) if (patterndispmode & 2) { if (!pattern[epnum[c]][p*4+1]) - memset(&textbuffer[8], '.', 2); + std::memset(&textbuffer[8], '.', 2); if (!pattern[epnum[c]][p*4+2]) - memset(&textbuffer[10], '.', 3); + std::memset(&textbuffer[10], '.', 3); } } } @@ -291,22 +329,22 @@ void printstatus(void) } } - sprintf(textbuffer, "CHN ORDERLIST (SUBTUNE "); + std::sprintf(textbuffer, "CHN ORDERLIST (SUBTUNE "); printtext(dpos.orderlistX, dpos.orderlistY, colors.CTITLE, textbuffer); - sprintf(textbuffer, "%02X", esnum); + std::sprintf(textbuffer, "%02X", esnum); printtext(dpos.orderlistX+23, dpos.orderlistY, colors.CEDIT, textbuffer); - sprintf(textbuffer, ", POS "); + std::sprintf(textbuffer, ", POS "); printtext(dpos.orderlistX+25, dpos.orderlistY, colors.CTITLE, textbuffer); - sprintf(textbuffer, "%02X", eseditpos); + std::sprintf(textbuffer, "%02X", eseditpos); printtext(dpos.orderlistX+31, dpos.orderlistY, colors.CEDIT, textbuffer); - sprintf(textbuffer, ")"); + std::sprintf(textbuffer, ")"); printtext(dpos.orderlistX+33, dpos.orderlistY, colors.CTITLE, textbuffer); - for (c = 0; c < maxChns; c++) + for (int c = 0; c < maxChns; c++) { - sprintf(textbuffer, " %d ", c+1); + std::sprintf(textbuffer, " %d ", c+1); printtext(dpos.orderlistX, dpos.orderlistY+1+c, colors.CTITLE, textbuffer); - for (d = 0; d < visibleOrderlist; d++) + for (int d = 0; d < visibleOrderlist; d++) { int p = esview+d; unsigned char currentSongorder = 0; @@ -321,7 +359,7 @@ void printstatus(void) currentSongorder = songorder_stereo[esnum][c][p]; currentSonglen = songlen_stereo[esnum][c]; } - color = colors.CNORMAL; + int color = colors.CNORMAL; if (isplaying()) { int chnpos = chn[c].songptr; @@ -334,7 +372,7 @@ void printstatus(void) if ((p < 0) || (p > (currentSonglen+1)) || (p > MAX_SONGLEN+1)) { - sprintf(textbuffer, " "); + std::sprintf(textbuffer, " "); } else { @@ -342,26 +380,26 @@ void printstatus(void) { if ((currentSongorder < REPEAT) || (p >= currentSonglen)) { - sprintf(textbuffer, "%02X ", currentSongorder); + std::sprintf(textbuffer, "%02X ", currentSongorder); if ((p >= currentSonglen) && (color == colors.CNORMAL)) color = colors.CCOMMAND; } else { if (currentSongorder >= TRANSUP) { - sprintf(textbuffer, "+%01X ", currentSongorder&0xf); + std::sprintf(textbuffer, "+%01X ", currentSongorder&0xf); if (color == colors.CNORMAL) color = colors.CCOMMAND; } else { if (currentSongorder >= TRANSDOWN) { - sprintf(textbuffer, "-%01X ", 16-(currentSongorder & 0x0f)); + std::sprintf(textbuffer, "-%01X ", 16-(currentSongorder & 0x0f)); if (color == colors.CNORMAL) color = colors.CCOMMAND; } else { - sprintf(textbuffer, "R%01X ", (currentSongorder+1) & 0x0f); + std::sprintf(textbuffer, "R%01X ", (currentSongorder+1) & 0x0f); if (color == colors.CNORMAL) color = colors.CCOMMAND; } } @@ -369,7 +407,7 @@ void printstatus(void) } if (currentSongorder == LOOPSONG) { - sprintf(textbuffer, "RST"); + std::sprintf(textbuffer, "RST"); if (color == colors.CNORMAL) color = colors.CCOMMAND; } } @@ -405,42 +443,43 @@ void printstatus(void) } } - sprintf(textbuffer, "INSTRUMENT NUM. %02X %-16s", einum, instr[einum].name); + std::sprintf(textbuffer, "INSTRUMENT NUM. %02X %-16s", einum, instr[einum].name); printtext(dpos.instrumentsX, dpos.instrumentsY, colors.CTITLE, textbuffer); - sprintf(textbuffer, "Attack/Decay %02X", instr[einum].ad); + int color; + std::sprintf(textbuffer, "Attack/Decay %02X", instr[einum].ad); if (eipos == 0) color = colors.CEDIT; else color = colors.CNORMAL; printtext(dpos.instrumentsX, dpos.instrumentsY+1, color, textbuffer); - sprintf(textbuffer, "Sustain/Release %02X", instr[einum].sr); + std::sprintf(textbuffer, "Sustain/Release %02X", instr[einum].sr); if (eipos == 1) color = colors.CEDIT; else color = colors.CNORMAL; printtext(dpos.instrumentsX, dpos.instrumentsY+2, color, textbuffer); - sprintf(textbuffer, "Wavetable Pos %02X", instr[einum].ptr[WTBL]); + std::sprintf(textbuffer, "Wavetable Pos %02X", instr[einum].ptr[WTBL]); if (eipos == 2) color = colors.CEDIT; else color = colors.CNORMAL; printtext(dpos.instrumentsX, dpos.instrumentsY+3, color, textbuffer); - sprintf(textbuffer, "Pulsetable Pos %02X", instr[einum].ptr[PTBL]); + std::sprintf(textbuffer, "Pulsetable Pos %02X", instr[einum].ptr[PTBL]); if (eipos == 3) color = colors.CEDIT; else color = colors.CNORMAL; printtext(dpos.instrumentsX, dpos.instrumentsY+4, color, textbuffer); - sprintf(textbuffer, "Filtertable Pos %02X", instr[einum].ptr[FTBL]); + std::sprintf(textbuffer, "Filtertable Pos %02X", instr[einum].ptr[FTBL]); if (eipos == 4) color = colors.CEDIT; else color = colors.CNORMAL; printtext(dpos.instrumentsX, dpos.instrumentsY+5, color, textbuffer); - sprintf(textbuffer, "Vibrato Param %02X", instr[einum].ptr[STBL]); + std::sprintf(textbuffer, "Vibrato Param %02X", instr[einum].ptr[STBL]); if (eipos == 5) color = colors.CEDIT; else color = colors.CNORMAL; printtext(dpos.instrumentsX+20, dpos.instrumentsY+1, color, textbuffer); - sprintf(textbuffer, "Vibrato Delay %02X", instr[einum].vibdelay); + std::sprintf(textbuffer, "Vibrato Delay %02X", instr[einum].vibdelay); if (eipos == 6) color = colors.CEDIT; else color = colors.CNORMAL; printtext(dpos.instrumentsX+20, dpos.instrumentsY+2, color, textbuffer); - sprintf(textbuffer, "HR/Gate Timer %02X", instr[einum].gatetimer); + std::sprintf(textbuffer, "HR/Gate Timer %02X", instr[einum].gatetimer); if (eipos == 7) color = colors.CEDIT; else color = colors.CNORMAL; printtext(dpos.instrumentsX+20, dpos.instrumentsY+3, color, textbuffer); - sprintf(textbuffer, "1stFrame Wave %02X", instr[einum].firstwave); + std::sprintf(textbuffer, "1stFrame Wave %02X", instr[einum].firstwave); if (eipos == 8) color = colors.CEDIT; else color = colors.CNORMAL; printtext(dpos.instrumentsX+20, dpos.instrumentsY+4, color, textbuffer); @@ -452,16 +491,16 @@ void printstatus(void) } else { - if (!eamode) printbg(dpos.instrumentsX+20+strlen(instr[einum].name), dpos.instrumentsY, cc, 1); + if (!eamode) printbg(dpos.instrumentsX+20+std::strlen(instr[einum].name), dpos.instrumentsY, cc, 1); } } - sprintf(textbuffer, "WAVE TBL PULSETBL FILT.TBL SPEEDTBL"); + std::sprintf(textbuffer, "WAVE TBL PULSETBL FILT.TBL SPEEDTBL"); printtext(dpos.instrumentsX, dpos.instrumentsY+7, colors.CTITLE, textbuffer); - for (c = 0; c < MAX_TABLES; c++) + for (int c = 0; c < MAX_TABLES; c++) { - for (d = 0; d < VISIBLETABLEROWS; d++) + for (int d = 0; d < VISIBLETABLEROWS; d++) { int p = etview[c]+d; @@ -481,13 +520,13 @@ void printstatus(void) break; } if ((p == etpos) && (etnum == c)) color = colors.CEDIT; - sprintf(textbuffer, "%02X:%02X %02X", p+1, ltable[c][p], rtable[c][p]); + std::sprintf(textbuffer, "%02X:%02X %02X", p+1, ltable[c][p], rtable[c][p]); if (patterndispmode & 2) { if (!ltable[c][p] && !rtable[c][p] && !ltable[c][p+1] && !rtable[c][p+1]) { - memset(&textbuffer[3], '.', 2); - memset(&textbuffer[6], '.', 2); + std::memset(&textbuffer[3], '.', 2); + std::memset(&textbuffer[6], '.', 2); } } printtext(dpos.instrumentsX+10*c, dpos.instrumentsY+8+d, color, textbuffer); @@ -514,15 +553,15 @@ void printstatus(void) } printtext(dpos.instrumentsX, dpos.instrumentsY+8+VISIBLETABLEROWS+1, colors.CTITLE, "NAME "); - sprintf(textbuffer, "%-32s", songname); + std::sprintf(textbuffer, "%-32s", songname); printtext(dpos.instrumentsX+9, dpos.instrumentsY+8+VISIBLETABLEROWS+1, colors.CEDIT, textbuffer); printtext(dpos.instrumentsX, dpos.instrumentsY+8+VISIBLETABLEROWS+2, colors.CTITLE, "AUTHOR "); - sprintf(textbuffer, "%-32s", authorname); + std::sprintf(textbuffer, "%-32s", authorname); printtext(dpos.instrumentsX+9, dpos.instrumentsY+8+VISIBLETABLEROWS+2, colors.CEDIT, textbuffer); printtext(dpos.instrumentsX, dpos.instrumentsY+8+VISIBLETABLEROWS+3, colors.CTITLE, "RELEASED "); - sprintf(textbuffer, "%-32s", copyrightname); + std::sprintf(textbuffer, "%-32s", copyrightname); printtext(dpos.instrumentsX+9, dpos.instrumentsY+8+VISIBLETABLEROWS+3, colors.CEDIT, textbuffer); if ((editmode == EDIT_NAMES) && (!eamode)) @@ -530,17 +569,17 @@ void printstatus(void) switch(enpos) { case 0: - printbg(dpos.instrumentsX+9+strlen(songname), dpos.instrumentsY+8+VISIBLETABLEROWS+1, cc, 1); + printbg(dpos.instrumentsX+9+std::strlen(songname), dpos.instrumentsY+8+VISIBLETABLEROWS+1, cc, 1); break; case 1: - printbg(dpos.instrumentsX+9+strlen(authorname), dpos.instrumentsY+8+VISIBLETABLEROWS+2, cc, 1); + printbg(dpos.instrumentsX+9+std::strlen(authorname), dpos.instrumentsY+8+VISIBLETABLEROWS+2, cc, 1); break; case 2: - printbg(dpos.instrumentsX+9+strlen(copyrightname), dpos.instrumentsY+8+VISIBLETABLEROWS+3, cc, 1); + printbg(dpos.instrumentsX+9+std::strlen(copyrightname), dpos.instrumentsY+8+VISIBLETABLEROWS+3, cc, 1); break; } } - sprintf(textbuffer, "OCTAVE %d", epoctave); + std::sprintf(textbuffer, "OCTAVE %d", epoctave); printtext(dpos.octaveX, dpos.octaveY, colors.CTITLE, textbuffer); switch(autoadvance) @@ -566,16 +605,16 @@ void printstatus(void) if (multiplier) { if (!ntsc) - sprintf(textbuffer, " %02d%c%02d ", timemin, timechar[timeframe/(25*multiplier) & 1], timesec); + std::sprintf(textbuffer, " %02d%c%02d ", timemin, timechar[timeframe/(25*multiplier) & 1], timesec); else - sprintf(textbuffer, " %02d%c%02d ", timemin, timechar[timeframe/(30*multiplier) & 1], timesec); + std::sprintf(textbuffer, " %02d%c%02d ", timemin, timechar[timeframe/(30*multiplier) & 1], timesec); } else { if (!ntsc) - sprintf(textbuffer, " %02d%c%02d ", timemin, timechar[(timeframe/13) & 1], timesec); + std::sprintf(textbuffer, " %02d%c%02d ", timemin, timechar[(timeframe/13) & 1], timesec); else - sprintf(textbuffer, " %02d%c%02d ", timemin, timechar[(timeframe/15) & 1], timesec); + std::sprintf(textbuffer, " %02d%c%02d ", timemin, timechar[(timeframe/15) & 1], timesec); } printtext(dpos.octaveX+10, dpos.octaveY+1, colors.CEDIT, textbuffer); @@ -598,7 +637,7 @@ void printstatus(void) " CHN1 CHN2 CHN3 CHN4 CHN5 CHN6 " ); } - for (c = 0; c < maxChns; c++) + for (int c = 0; c < maxChns; c++) { int chnpos = chn[c].songptr; int chnrow = chn[c].pattptr/4; @@ -607,7 +646,7 @@ void printstatus(void) if (chnrow > pattlen[chn[c].pattnum]) chnrow = pattlen[chn[c].pattnum]; if (chnrow >= 100) chnrow -= 100; - sprintf(textbuffer, "%03X/%02X", + std::sprintf(textbuffer, "%03X/%02X", chnpos,chnrow); printtext(dpos.channelsX+7*c, dpos.channelsY+1, chn[c].mute ? colors.CMUTE : colors.CEDIT, textbuffer); } @@ -617,14 +656,14 @@ void printstatus(void) } -void resettime(void) +void resettime() { timemin = 0; timesec = 0; timeframe = 0; } -void incrementtime(void) +void incrementtime() { { timeframe++; diff --git a/src/display.h b/src/display.h new file mode 100644 index 0000000..5364ab4 --- /dev/null +++ b/src/display.h @@ -0,0 +1,29 @@ +/* + * LoadTracker + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef DISPLAY_H +#define DISPLAY_H + +void initcolorscheme(bool dark); +void printmainscreen(); +void displayupdate(); +void printstatus(); +void resettime(); +void incrementtime(); + +#endif diff --git a/src/gfile.c b/src/file.cpp similarity index 69% rename from src/gfile.c rename to src/file.cpp index 9e295fc..a1bf8f0 100644 --- a/src/gfile.c +++ b/src/file.cpp @@ -1,92 +1,115 @@ /* - * ============================================================================= - * file selector - * ============================================================================= + * LoadTracker + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#define GFILE_C +// ============================================================================= +// file selector +// ============================================================================= + +#define FILE_C + +#include "loadtrk.h" +#include "bme_main.h" +#include "bme_win.h" + +#include +#include +#include +#include + +#include +#include +#include +#include #ifdef __WIN32__ #include #endif -#include "loadtrk.h" +#define MAX_DIRFILES 16384 +#define DOUBLECLICKDELAY 15 + +typedef struct +{ + char *name; + int attribute; +} DIRENTRY; DIRENTRY direntry[MAX_DIRFILES]; -void initpaths(void) +int cmpname(char *string1, char *string2); + +void initpaths() { - int c; - - for (c = 0; c < MAX_DIRFILES; c++) - direntry[c].name = NULL; - - memset(loadedsongfilename, 0, sizeof loadedsongfilename); - memset(songfilename, 0, sizeof songfilename); - memset(instrfilename, 0, sizeof instrfilename); - memset(songpath, 0, sizeof songpath); - memset(instrpath, 0, sizeof instrpath); - memset(packedpath, 0, sizeof packedpath); - strcpy(songfilter, "*.sng"); - strcpy(instrfilter, "*.ins"); + for (int c = 0; c < MAX_DIRFILES; c++) + direntry[c].name = nullptr; + + std::memset(loadedsongfilename, 0, sizeof loadedsongfilename); + std::memset(songfilename, 0, sizeof songfilename); + std::memset(instrfilename, 0, sizeof instrfilename); + std::memset(songpath, 0, sizeof songpath); + std::memset(instrpath, 0, sizeof instrpath); + std::memset(packedpath, 0, sizeof packedpath); + std::strcpy(songfilter, "*.sng"); + std::strcpy(instrfilter, "*.ins"); getcwd(songpath, MAX_PATHNAME); - strcpy(instrpath, songpath); - strcpy(packedpath, songpath); + std::strcpy(instrpath, songpath); + std::strcpy(packedpath, songpath); } -int fileselector(char *name, char *path, char *filter, char *title, int filemode) +int fileselector(char *name, char *path, char *filter, const char *title, int filemode) { - size_t i; - int c, d, scrrep; - int color; - int files; - int filepos = 0; - int fileview = 0; - int lastclick = 0; - int lastfile = 0; - int lowest; - int exitfilesel; - - DIR *dir; - struct dirent *de; - struct stat st; - #ifdef __WIN32__ +#ifdef __WIN32__ char drivestr[] = "A:\\"; char driveexists[26]; - #endif +#endif char cmpbuf[MAX_PATHNAME]; char tempname[MAX_PATHNAME]; // Set initial path (if any) - if (strlen(path)) chdir(path); + if (std::strlen(path)) chdir(path); // Scan for all existing drives - #ifdef __WIN32__ - for (c = 0; c < 26; c++) +#ifdef __WIN32__ + for (int c = 0; c < 26; c++) { drivestr[0] = 'A'+c; if (GetDriveType(drivestr) > 1) driveexists[c] = 1; else driveexists[c] = 0; } - #endif +#endif // Read new directory NEWPATH: getcwd(path, MAX_PATHNAME); - files = 0; + int files = 0; // Deallocate old names - for (c = 0; c < MAX_DIRFILES; c++) + for (int c = 0; c < MAX_DIRFILES; c++) { if (direntry[c].name) { - free(direntry[c].name); - direntry[c].name = NULL; + std::free(direntry[c].name); + direntry[c].name = nullptr; } } - #ifdef __WIN32__ +#ifdef __WIN32__ // Create drive letters - for (c = 0; c < 26; c++) + for (int c = 0; c < 26; c++) { if (driveexists[c]) { @@ -96,26 +119,29 @@ int fileselector(char *name, char *path, char *filter, char *title, int filemode files++; } } - #endif +#endif // Process directory - #ifdef __amigaos__ + DIR *dir; +#ifdef __amigaos__ dir = opendir(""); - #else +#else dir = opendir("."); - #endif +#endif if (dir) { - char *filtptr = strstr(filter, "*"); + char *filtptr = std::strstr(filter, "*"); if (!filtptr) filtptr = filter; else filtptr++; - for (i = 0; i < strlen(filter); i++) - filter[i] = tolower(filter[i]); + for (size_t i = 0; i < std::strlen(filter); i++) + filter[i] = std::tolower(filter[i]); + struct dirent *de; while ((de = readdir(dir))) { - if ((files < MAX_DIRFILES) && (strlen(de->d_name) < MAX_FILENAME)) + if ((files < MAX_DIRFILES) && (std::strlen(de->d_name) < MAX_FILENAME)) { + struct stat st; direntry[files].name = strdup(de->d_name); direntry[files].attribute = 0; stat(de->d_name, &st); @@ -128,18 +154,18 @@ int fileselector(char *name, char *path, char *filter, char *title, int filemode { // If a file, must match filter strcpy(cmpbuf, de->d_name); - if ((!strcmp(filtptr, "*")) || (!strcmp(filtptr, ".*"))) + if ((!std::strcmp(filtptr, "*")) || (!strcmp(filtptr, ".*"))) files++; else { - for (i = 0; i < strlen(cmpbuf); i++) - cmpbuf[i] = tolower(cmpbuf[i]); - if (strstr(cmpbuf, filtptr)) + for (size_t i = 0; i < strlen(cmpbuf); i++) + cmpbuf[i] = std::tolower(cmpbuf[i]); + if (std::strstr(cmpbuf, filtptr)) files++; else { - free(direntry[files].name); - direntry[files].name = NULL; + std::free(direntry[files].name); + direntry[files].name = nullptr; } } } @@ -148,10 +174,10 @@ int fileselector(char *name, char *path, char *filter, char *title, int filemode closedir(dir); } // Sort the filelist in a most horrible fashion - for (c = 0; c < files; c++) + for (int c = 0; c < files; c++) { - lowest = c; - for (d = c+1; d < files; d++) + int lowest = c; + for (int d = c+1; d < files; d++) { if (direntry[d].attribute < direntry[lowest].attribute) { @@ -177,9 +203,12 @@ int fileselector(char *name, char *path, char *filter, char *title, int filemode } // Search for the current filename - fileview = 0; - filepos = 0; - for (c = 0; c < files; c++) + int fileview = 0; + int filepos = 0; + int lastclick = 0; + int lastfile = 0; + + for (int c = 0; c < files; c++) { if ((!direntry[c].attribute) && (!cmpname(name, direntry[c].name))) { @@ -187,7 +216,7 @@ int fileselector(char *name, char *path, char *filter, char *title, int filemode } } - exitfilesel = -1; + int exitfilesel = -1; while (exitfilesel < 0) { int cc = cursorcolortable[cursorflash]; @@ -204,12 +233,12 @@ int fileselector(char *name, char *path, char *filter, char *title, int filemode if (win_quitted) { exitprogram = 1; - for (c = 0; c < MAX_DIRFILES; c++) + for (int c = 0; c < MAX_DIRFILES; c++) { if (direntry[c].name) { - free(direntry[c].name); - direntry[c].name = NULL; + std::free(direntry[c].name); + direntry[c].name = nullptr; } } return 0; @@ -249,15 +278,15 @@ int fileselector(char *name, char *path, char *filter, char *title, int filemode { if (((key >= '0') && (key <= '0')) || ((key >= 'a') && (key <= 'z')) || ((key >= 'A') && (key <= 'Z'))) { - char k = tolower(key); + char k = std::tolower(key); int oldfilepos = filepos; for (filepos = oldfilepos + 1; filepos < files; filepos++) - if (tolower(direntry[filepos].name[0]) == k) break; + if (std::tolower(direntry[filepos].name[0]) == k) break; if (filepos >= files) { for (filepos = 0; filepos < oldfilepos; filepos++) - if (tolower(direntry[filepos].name[0]) == k) break; + if (std::tolower(direntry[filepos].name[0]) == k) break; } if (!direntry[filepos].attribute) strcpy(name, direntry[filepos].name); @@ -273,11 +302,11 @@ int fileselector(char *name, char *path, char *filter, char *title, int filemode case KEY_BACKSPACE: if (!filemode) { - #ifdef __amigaos__ +#ifdef __amigaos__ chdir("/"); - #else +#else chdir(".."); - #endif +#endif goto NEWPATH; } break; @@ -299,7 +328,7 @@ int fileselector(char *name, char *path, char *filter, char *title, int filemode break; case KEY_PGUP: - for (scrrep = PGUPDNREPEAT; scrrep; scrrep--) + for (int scrrep = PGUPDNREPEAT; scrrep; scrrep--) { if ((!filemode) && (filepos > 0)) { @@ -318,7 +347,7 @@ int fileselector(char *name, char *path, char *filter, char *title, int filemode break; case KEY_PGDN: - for (scrrep = PGUPDNREPEAT; scrrep; scrrep--) + for (int scrrep = PGUPDNREPEAT; scrrep; scrrep--) { if ((!filemode) && (filepos < files-1)) { @@ -412,7 +441,7 @@ int fileselector(char *name, char *path, char *filter, char *title, int filemode // Refresh fileselector display if (isplaying()) printstatus(); - for (c = 0; c < VISIBLEFILES+7; c++) + for (int c = 0; c < VISIBLEFILES+7; c++) { printblank(dpos.loadboxX-(MAX_FILENAME+10)/2, dpos.loadboxY+c, MAX_FILENAME+10); } @@ -420,30 +449,30 @@ int fileselector(char *name, char *path, char *filter, char *title, int filemode printblankc(dpos.loadboxX-(MAX_FILENAME+10)/2+1, dpos.loadboxY+1, colors.CHEADER, MAX_FILENAME+8); printtext(dpos.loadboxX-(MAX_FILENAME+10)/2+1, dpos.loadboxY+1, colors.CHEADER, title); - for (c = 0; c < VISIBLEFILES; c++) + for (int c = 0; c < VISIBLEFILES; c++) { if ((fileview+c >= 0) && (fileview+c < files)) { switch (direntry[fileview+c].attribute) { case 0: - sprintf(textbuffer, "%-60s ", direntry[fileview+c].name); + std::sprintf(textbuffer, "%-60s ", direntry[fileview+c].name); break; case 1: - sprintf(textbuffer, "%-60s ", direntry[fileview+c].name); + std::sprintf(textbuffer, "%-60s ", direntry[fileview+c].name); break; case 2: - sprintf(textbuffer, "%-60s ", direntry[fileview+c].name); + std::sprintf(textbuffer, "%-60s ", direntry[fileview+c].name); break; } } else { - sprintf(textbuffer, " "); + std::sprintf(textbuffer, " "); } - color = colors.CNORMAL; + int color = colors.CNORMAL; if ((fileview+c) == filepos) color = colors.CEDIT; textbuffer[68] = 0; printtext(dpos.loadboxX-(MAX_FILENAME+10)/2+1, dpos.loadboxY+2+c, color, textbuffer); @@ -451,21 +480,21 @@ int fileselector(char *name, char *path, char *filter, char *title, int filemode } printtext(dpos.loadboxX-(MAX_FILENAME+10)/2+1, dpos.loadboxY+3+VISIBLEFILES, 15, "PATH: "); - sprintf(textbuffer, "%-60s", path); + std::sprintf(textbuffer, "%-60s", path); textbuffer[MAX_FILENAME] = 0; - color = (filemode == 1) ? colors.CEDIT : colors.CNORMAL; + int color = (filemode == 1) ? colors.CEDIT : colors.CNORMAL; printtext(dpos.loadboxX-(MAX_FILENAME+10)/2+9, dpos.loadboxY+3+VISIBLEFILES, color, textbuffer); if ((filemode == 1) && (strlen(path) < MAX_FILENAME)) printbg(dpos.loadboxX-(MAX_FILENAME+10)/2+9+strlen(path), dpos.loadboxY+3+VISIBLEFILES, cc, 1); printtext(dpos.loadboxX-(MAX_FILENAME+10)/2+1, dpos.loadboxY+4+VISIBLEFILES, 15, "FILTER: "); - sprintf(textbuffer, "%-60s", filter); + std::sprintf(textbuffer, "%-60s", filter); textbuffer[MAX_FILENAME] = 0; color = (filemode == 2) ? colors.CEDIT : colors.CNORMAL; printtext(dpos.loadboxX-(MAX_FILENAME+10)/2+9, dpos.loadboxY+4+VISIBLEFILES, color, textbuffer); if (filemode == 2) printbg(dpos.loadboxX-(MAX_FILENAME+10)/2+9+strlen(filter), dpos.loadboxY+4+VISIBLEFILES, cc, 1); printtext(dpos.loadboxX-(MAX_FILENAME+10)/2+1, dpos.loadboxY+5+VISIBLEFILES, 15, "NAME: "); - sprintf(textbuffer, "%-60s", name); + std::sprintf(textbuffer, "%-60s", name); textbuffer[MAX_FILENAME] = 0; color = (filemode == 3) ? colors.CEDIT : colors.CNORMAL; printtext(dpos.loadboxX-(MAX_FILENAME+10)/2+9, dpos.loadboxY+5+VISIBLEFILES, color, textbuffer); @@ -481,12 +510,12 @@ int fileselector(char *name, char *path, char *filter, char *title, int filemode } // Deallocate all used names - for (c = 0; c < MAX_DIRFILES; c++) + for (int c = 0; c < MAX_DIRFILES; c++) { if (direntry[c].name) { - free(direntry[c].name); - direntry[c].name = NULL; + std::free(direntry[c].name); + direntry[c].name = nullptr; } } @@ -495,31 +524,12 @@ int fileselector(char *name, char *path, char *filter, char *title, int filemode return exitfilesel; } -void editstring(char *buffer, int maxlength) -{ - int len = strlen(buffer); - - if ((key >= 32) && (key < 256)) - { - if (len < maxlength-1) - { - buffer[len] = key; - buffer[len+1] = 0; - } - } - - if ((rawkey == KEY_BACKSPACE) && (len > 0)) - { - buffer[len-1] = 0; - } -} - int cmpname(char *string1, char *string2) { for (;;) { - unsigned char char1 = tolower(*string1++); - unsigned char char2 = tolower(*string2++); + unsigned char char1 = std::tolower(*string1++); + unsigned char char2 = std::tolower(*string2++); if (char1 < char2) return -1; if (char1 > char2) return 1; if ((!char1) || (!char2)) return 0; diff --git a/src/file.h b/src/file.h new file mode 100644 index 0000000..ee26adc --- /dev/null +++ b/src/file.h @@ -0,0 +1,29 @@ +/* + * LoadTracker + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef FILE_H +#define FILE_H + +#define MAX_FILENAME 60 +#define MAX_PATHNAME 256 + +void initpaths(); +int fileselector(char *name, char *path, char *filter, const char *title, int filemode); + +#endif + diff --git a/src/gconsole.h b/src/gconsole.h deleted file mode 100644 index 2e8f217..0000000 --- a/src/gconsole.h +++ /dev/null @@ -1,54 +0,0 @@ -#ifndef GCONSOLE_H -#define GCONSOLE_H - -#define MAX_COLUMNS 128 -#define MAX_ROWS 40 -#define HOLDDELAY 24 -#define DOUBLECLICKDELAY 15 - -typedef struct -{ - int channelsX; - int channelsY; - int instrumentsX; - int instrumentsY; - int loadboxX; - int loadboxY; - int octaveX; - int octaveY; - int orderlistX; - int orderlistY; - int patternsX; - int patternsY; - int statusBottomX; - int statusBottomY; - int statusTopEndX; - int statusTopFvX; - int statusTopX; - int statusTopY; -} POSITIONS; - - -int initscreen(void); -void closescreen(void); -void clearscreen(void); -void fliptoscreen(void); -void printtext(int x, int y, int color, const char *text); -void printtextc(int y, int color, const char *text); -void printtextcp(int cp, int y, int color, const char *text); -void printblank(int x, int y, int length); -void printblankc(int x, int y, int color, int length); -void drawbox(int x, int y, int color, int sx, int sy); -void printbg(int x, int y, int color, int length); -void getkey(void); -void initDisplayPositions(void); - -#ifndef GCONSOLE_C -extern int key, rawkey, shiftpressed, altpressed, cursorflashdelay; -extern int mouseb, prevmouseb; -extern int mouseheld; -extern int mousex, mousey; -extern POSITIONS dpos; -#endif - -#endif diff --git a/src/gdisplay.h b/src/gdisplay.h deleted file mode 100644 index 241e2de..0000000 --- a/src/gdisplay.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef GDISPLAY_H -#define GDISPLAY_H - -#define CBLACK 0x0 -#define CDBLUE 0x1 -#define CDGREEN 0x2 -#define CDGREY 0x3 -#define CDRED 0x4 -#define CDBROWN 0x5 -#define CLBROWN 0x6 -#define CLGREY 0x7 -#define CGREY 0x8 -#define CLBLUE 0x9 -#define CLGREEN 0xA -#define CCYAN 0xB -#define CLRED 0xC -#define CPURPLE 0xD -#define CYELLOW 0xE -#define CWHITE 0xF - -void initcolorscheme(int dark); -void printmainscreen(void); -void displayupdate(void); -void printstatus(void); -void resettime(void); -void incrementtime(void); - -#endif diff --git a/src/gfile.h b/src/gfile.h deleted file mode 100644 index 561221c..0000000 --- a/src/gfile.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef GFILE_H -#define GFILE_H - -#define MAX_DIRFILES 16384 -#define MAX_FILENAME 60 -#define MAX_PATHNAME 256 - -typedef struct -{ - char *name; - int attribute; -} DIRENTRY; - -void initpaths(void); -int fileselector(char *name, char *path, char *filter, char *title, int filemode); -void editstring(char *buffer, int maxlength); -int cmpname(char *string1, char *string2); - -#endif - diff --git a/src/ginstr.h b/src/ginstr.h deleted file mode 100644 index 04c46c4..0000000 --- a/src/ginstr.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef GINSTR_H -#define GINSTR_H - -#ifndef GINSTR_C -extern int einum; -extern int eipos; -extern int eicolumn; -extern INSTR instrcopybuffer; -#endif - -void instrumentcommands(void); -void nextinstr(void); -void previnstr(void); -void clearinstr(int num); -void gotoinstr(int i); -void showinstrtable(void); - -#endif diff --git a/src/gorder.h b/src/gorder.h deleted file mode 100644 index 5440ad5..0000000 --- a/src/gorder.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef GORDER_H -#define GORDER_H - -#ifndef GORDER_C -extern int espos[MAX_CHN]; -extern int esend[MAX_CHN]; -extern int eseditpos; -extern int esview; -extern int escolumn; -extern int eschn; -extern int esnum; -extern int esmarkchn; -extern int esmarkstart; -extern int esmarkend; -extern int enpos; -#endif - -void updateviewtopos(void); -void orderlistcommands(void); -void orderlistcommands_stereo(void); -void namecommands(void); -void nextsong(void); -void prevsong(void); -void songchange(void); -void orderleft(void); -void orderright(void); -void deleteorder(void); -void insertorder(unsigned char byte); -void orderleft_stereo(void); -void orderright_stereo(void); -void deleteorder_stereo(void); -void insertorder_stereo(unsigned char byte); - -#endif diff --git a/src/gpattern.h b/src/gpattern.h deleted file mode 100644 index 2891a4d..0000000 --- a/src/gpattern.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef GPATTERN_H -#define GPATTERN_H - -#ifndef GPATTERN_C -extern int epnum[MAX_CHN]; -extern int eppos; -extern int epview; -extern int epcolumn; -extern int epchn; -extern int epoctave; -extern int epmarkchn; -extern int epmarkstart; -extern int epmarkend; -#endif - -void patterncommands(void); -void nextpattern(void); -void prevpattern(void); -void patternup(void); -void patterndown(void); -void shrinkpattern(void); -void expandpattern(void); -void splitpattern(void); -void joinpattern(void); - -#endif diff --git a/src/gplay.h b/src/gplay.h deleted file mode 100644 index de92e68..0000000 --- a/src/gplay.h +++ /dev/null @@ -1,62 +0,0 @@ -#ifndef GPLAY_H -#define GPLAY_H - -#define PLAY_PLAYING 0x00 -#define PLAY_BEGINNING 0x01 -#define PLAY_POS 0x02 -#define PLAY_PATTERN 0x03 -#define PLAY_STOP 0x04 -#define PLAY_STOPPED 0x80 - -typedef struct -{ - unsigned char trans; - unsigned char instr; - unsigned char note; - unsigned char lastnote; - unsigned char newnote; - int pattptr; - unsigned char pattnum; - unsigned char songptr; - unsigned char repeat; - unsigned short freq; - unsigned char gate; - unsigned char wave; - unsigned short pulse; - unsigned char ptr[2]; - unsigned char pulsetime; - unsigned char wavetime; - unsigned char vibtime; - unsigned char vibdelay; - unsigned char command; - unsigned char cmddata; - unsigned char newcommand; - unsigned char newcmddata; - unsigned char tick; - unsigned char tempo; - unsigned char mute; - unsigned char advance; - unsigned char gatetimer; -} CHN; - -#ifndef GPLAY_C -extern CHN chn[MAX_CHN]; -extern unsigned char masterfader; -extern unsigned char freqtbllo[]; -extern unsigned char freqtblhi[]; -extern int lastsonginit; -#endif - -void initchannels(void); -void initsong(int num, int playmode); -void initsongpos(int num, int playmode, int pattpos); -void stopsong(void); -void rewindsong(void); -void playtestnote(int note, int ins, int chnnum); -void releasenote(int chnnum); -void mutechannel(int chnnum); -int isplaying(void); -void playroutine(void); -void playroutine_stereo(void); - -#endif diff --git a/src/greloc.h b/src/greloc.h deleted file mode 100644 index 507a0ee..0000000 --- a/src/greloc.h +++ /dev/null @@ -1,57 +0,0 @@ -#ifndef GRELOC_H -#define GRELOC_H - -#define FORMAT_SID 0 -#define FORMAT_PRG 1 -#define FORMAT_BIN 2 - -#define PLAYER_BUFFERED 8 -#define PLAYER_SOUNDEFFECTS 16 -#define PLAYER_VOLUME 32 -#define PLAYER_AUTHORINFO 64 -#define PLAYER_ZPGHOSTREGS 128 -#define PLAYER_NOOPTIMIZATION 256 -#define PLAYER_FULLBUFFERED 512 - -#define MAX_OPTIONS 7 - -#define TYPE_NONE 0 -#define TYPE_OVERFLOW 1 -#define TYPE_JUMP 2 - -#define CAUSE_NONE 0 -#define CAUSE_PATTERN 1 -#define CAUSE_INSTRUMENT 2 -#define CAUSE_WAVECMD 3 - -#define MAX_BYTES_PER_ROW 16 - -#ifndef GRELOC_C -extern unsigned char pattused[MAX_PATT]; -extern unsigned char instrused[MAX_INSTR]; -extern unsigned char tableused[MAX_TABLES][MAX_TABLELEN+1]; -extern unsigned char pattmap[MAX_PATT]; -extern unsigned char instrmap[MAX_INSTR]; -extern unsigned char tablemap[MAX_TABLES][MAX_TABLELEN+1]; -extern int tableerror; -#endif - -void relocator(void); -void relocator_stereo(void); -int testoverlap(int area1start, int area1size, int area2start, int area2size); -int packpattern(unsigned char *dest, unsigned char *src, int rows); -unsigned char swapnybbles(unsigned char n); -void findtableduplicates(int num); -int isusedandselfcontained(int num, int start); -void calcspeedtest(unsigned char pos); - -int insertfile(char *name); -void inserttext(const char *text); -void insertdefine(const char *name, int value); -void insertlabel(const char *name); -void insertbyte(unsigned char byte); -void insertbytes(const unsigned char *bytes, int size); -void insertaddrlo(const char *name); -void insertaddrhi(const char *name); - -#endif diff --git a/src/gsid.h b/src/gsid.h deleted file mode 100644 index 9ea401b..0000000 --- a/src/gsid.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef GSID_H -#define GSID_H - -#define NUMSIDREGS 0x19 -#define SIDWRITEDELAY 14 // lda $xxxx,x 4 cycles, sta $d400,x 5 cycles, dex 2 cycles, bpl 3 cycles -#define SIDWAVEDELAY 4 // and $xxxx,x 4 cycles extra - -#ifdef __cplusplus -extern "C" { -#endif - -void sid_init(int speed, unsigned m, - unsigned ntsc, unsigned interpolate, - unsigned customclockrate, unsigned numsids, - float filterbias, unsigned combwaves); -int sid_fillbuffer(short *ptr, int samples); -int sid_fillbuffer_stereo(short *lptr, short *rptr, int samples); -unsigned char sid_getorder(unsigned char index); - -#ifndef GSID_C -extern unsigned char sidreg[NUMSIDREGS]; -extern unsigned char sidreg2[NUMSIDREGS]; -#endif - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/gsound.h b/src/gsound.h deleted file mode 100644 index 6705881..0000000 --- a/src/gsound.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef GSOUND_H -#define GSOUND_H - -#define MINMIXRATE 11025 -#define MAXMIXRATE 48000 -#define DEFAULTMIXRATE 48000 - -#define PALFRAMERATE 50 -#define PALCLOCKRATE 985248 -#define NTSCFRAMERATE 60 -#define NTSCCLOCKRATE 1022727 - -#define MIXBUFFERSIZE 65536 - -int sound_init(unsigned mr, unsigned writer, unsigned m, unsigned ntsc, - unsigned multiplier, unsigned interpolate, unsigned customclockrate, - unsigned exsid, float filterbias, unsigned combwaves); -void sound_uninit(void); - -#endif diff --git a/src/gtable.h b/src/gtable.h deleted file mode 100644 index ca34ddf..0000000 --- a/src/gtable.h +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef GTABLE_H -#define GTABLE_H - -#define MST_NOFINEVIB 0 -#define MST_FINEVIB 1 -#define MST_FUNKTEMPO 2 -#define MST_PORTAMENTO 3 -#define MST_RAW 4 - -#ifndef GTABLE_C -extern int etview[MAX_TABLES]; -extern int etnum; -extern int etpos; -extern int etcolumn; -extern int etlock; -extern int etmarknum; -extern int etmarkstart; -extern int etmarkend; -#endif - -void tablecommands(void); -void tableup(void); -void tabledown(void); -void inserttable(int num, int pos, int mode); -void deletetable(int num, int pos); -int makespeedtable(unsigned data, int mode, int makenew); -void optimizetable(int num); -void deleteinstrtable(int i); -int gettablelen(int num); -int gettablepartlen(int num, int pos); -void gototable(int num, int pos); -void settableview(int num, int pos); -void settableviewfirst(int num, int pos); -void validatetableview(void); -void exectable(int num, int ptr); -int findfreespeedtable(void); - -#endif diff --git a/src/ghelp.c b/src/help.cpp similarity index 93% rename from src/ghelp.c rename to src/help.cpp index 9a5fc77..0c2af15 100644 --- a/src/ghelp.c +++ b/src/help.cpp @@ -1,17 +1,37 @@ /* - * ============================================================================= - * online help - * ============================================================================= + * LoadTracker + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#define GHELP_C +// ============================================================================= +// online help +// ============================================================================= + +#define HELP_C #include "loadtrk.h" +#include "bme_main.h" +#include "bme_win.h" + +#include #define HELP_HEADER 15 #define HELP_NORMAL 7 -int printrows(int column, int row, int color, char *strings[] ) { +int printrows(int column, int row, int color, const char *strings[] ) { int n = 0; while(strings[n]) { printtext(column, row++, color, strings[n++]); @@ -21,7 +41,7 @@ int printrows(int column, int row, int color, char *strings[] ) { void onlinehelp(int standalone,int context) { - char *genkeys[] = { + const char *genkeys[] = { "F1 Play from beginning", "F2 Play from current position", "F3 Play current pattern", @@ -51,7 +71,7 @@ void onlinehelp(int standalone,int context) NULL }; - char *patternkeys[] = { + const char *patternkeys[] = { "Enter notes like on piano (PT or DMC)", "0-9 & A-F to enter commands", "SPC Switch between jam/editmode", @@ -78,7 +98,7 @@ void onlinehelp(int standalone,int context) NULL }; - char *songkeys[] = { + const char *songkeys[] = { "0-9 & A-F to enter pattern numbers", "SPC Set start position for F2 key", "BACKSPC Set end position for F2 key", @@ -93,7 +113,7 @@ void onlinehelp(int standalone,int context) NULL }; - char *instkeys[] = { + const char *instkeys[] = { "0-9 & A-F to enter parameters", "SPC Play test note", "SHIFT+SPC Silence test note", @@ -116,7 +136,7 @@ void onlinehelp(int standalone,int context) NULL }; - char *pattcmds[] = { + const char *pattcmds[] = { " ", "Command 0XY: Do nothing. Databyte will always be 00. ", " ", @@ -163,7 +183,7 @@ void onlinehelp(int standalone,int context) NULL }; - char *instparm[] = { + const char *instparm[] = { " ", "Attack/Decay 0 is fastest attack or decay, F is slowest ", " ", @@ -199,7 +219,7 @@ void onlinehelp(int standalone,int context) NULL }; - char *tables[] = { + const char *tables[] = { " ", "Wavetable left side: 00 Leave waveform unchanged ", " 01-0F Delay this step by 1-15 frames ", @@ -341,7 +361,7 @@ void onlinehelp(int standalone,int context) printblank(0, 0, MAX_COLUMNS); printbg(0, 0, colors.CHDRBG, MAX_COLUMNS); - sprintf(textbuffer, "%s Online Help", programname); + std::sprintf(textbuffer, "%s Online Help", programname); printtext(1, 0, colors.CHEADER, textbuffer); if(standalone) { printtext(84, 0, colors.CHEADER, "Arrows/PgUp/PgDn/Home/End scroll, ESC exits"); @@ -411,7 +431,7 @@ void onlinehelp(int standalone,int context) if (hview < -(lastrow-MAX_ROWS+1)) hview = -(lastrow-MAX_ROWS+1); if ((mouseb) && (!prevmouseb) && (!mousey)) break; } - EXITHELP: ; +EXITHELP: if(!standalone) { printmainscreen(); key = 0; diff --git a/src/ginstr.c b/src/instr.cpp similarity index 73% rename from src/ginstr.c rename to src/instr.cpp index e4f81c5..4bff5c3 100644 --- a/src/ginstr.c +++ b/src/instr.cpp @@ -1,12 +1,31 @@ /* - * ============================================================================= - * instrument editor - * ============================================================================= + * LoadTracker + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#define GINSTR_C +// ============================================================================= +// instrument editor +// ============================================================================= + +#define INSTR_C #include "loadtrk.h" +#include "bme_main.h" + +#include INSTR instrcopybuffer; int cutinstr = -1; @@ -15,11 +34,11 @@ int einum; int eipos; int eicolumn; -void instrumentcommands(void) +void instrumentcommands() { switch(rawkey) { - case 0x8: + case KEY_CANC: case KEY_DEL: if ((einum) && (shiftpressed) && (eipos < 9)) { @@ -32,7 +51,7 @@ void instrumentcommands(void) if ((einum) && (shiftpressed) && (eipos < 9)) { cutinstr = einum; - memcpy(&instrcopybuffer, &instr[einum], sizeof(INSTR)); + std::memcpy(&instrcopybuffer, &instr[einum], sizeof(INSTR)); clearinstr(einum); } break; @@ -41,20 +60,19 @@ void instrumentcommands(void) if ((einum) && (shiftpressed) && (eipos < 9)) { cutinstr = -1; - memcpy(&instrcopybuffer, &instr[einum], sizeof(INSTR)); + std::memcpy(&instrcopybuffer, &instr[einum], sizeof(INSTR)); } break; case KEY_S: if ((einum) && (shiftpressed) && (eipos < 9)) { - memcpy(&instr[einum], &instrcopybuffer, sizeof(INSTR)); + std::memcpy(&instr[einum], &instrcopybuffer, sizeof(INSTR)); if (cutinstr != -1) { - int c, d; - for (c = 0; c < MAX_PATT; c++) + for (int c = 0; c < MAX_PATT; c++) { - for (d = 0; d < pattlen[c]; d++) + for (int d = 0; d < pattlen[c]; d++) if (pattern[c][d*4+1] == cutinstr) pattern[c][d*4+1] = einum; } } @@ -64,7 +82,7 @@ void instrumentcommands(void) case KEY_V: if ((einum) && (shiftpressed) && (eipos < 9)) { - memcpy(&instr[einum], &instrcopybuffer, sizeof(INSTR)); + std::memcpy(&instr[einum], &instrcopybuffer, sizeof(INSTR)); } break; @@ -211,7 +229,7 @@ void instrumentcommands(void) void clearinstr(int num) { - memset(&instr[num], 0, sizeof(INSTR)); + std::memset(&instr[num], 0, sizeof(INSTR)); if (num) { if (multiplier) @@ -225,8 +243,7 @@ void clearinstr(int num) void gotoinstr(int i) { - if (i < 0) return; - if (i >= MAX_INSTR) return; + if ((i < 0) || (i >= MAX_INSTR)) return; einum = i; showinstrtable(); @@ -252,9 +269,7 @@ void showinstrtable(void) { if (!etlock) { - int c; - - for (c = MAX_TABLES-1; c >= 0; c--) + for (int c = MAX_TABLES-1; c >= 0; c--) { if (instr[einum].ptr[c]) settableviewfirst(c, instr[einum].ptr[c] - 1); diff --git a/src/instr.h b/src/instr.h new file mode 100644 index 0000000..8c7aec7 --- /dev/null +++ b/src/instr.h @@ -0,0 +1,36 @@ +/* + * LoadTracker + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef INSTR_H +#define INSTR_H + +#ifndef INSTR_C +extern int einum; +extern int eipos; +extern int eicolumn; +extern INSTR instrcopybuffer; +#endif + +void instrumentcommands(); +void nextinstr(); +void previnstr(); +void clearinstr(int num); +void gotoinstr(int i); +void showinstrtable(); + +#endif diff --git a/src/loadtrk.c b/src/loadtrk.cpp similarity index 87% rename from src/loadtrk.c rename to src/loadtrk.cpp index c0f6948..d95467e 100644 --- a/src/loadtrk.c +++ b/src/loadtrk.cpp @@ -1,20 +1,20 @@ -// -// LoadTracker -// -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation; either version 2 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -// +/* + * LoadTracker + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ #define LOADTRK_C @@ -23,14 +23,28 @@ #endif #include "loadtrk.h" -#include "bme.h" +#include "bme_main.h" +#include "bme_win.h" +#include "bme_snd.h" +#include "bme_io.h" #include +#include +#include +#include +#include +#include + +#include +#include + +#define HOLDDELAY 24 + // Increase if configuration has incompatible changes #define CFG_VERSION 2 -int menu = 0; +bool menu = false; int editmode = EDIT_PATTERN; int recordmode = 1; int followplay = 0; @@ -88,7 +102,7 @@ char instrpath[MAX_PATHNAME]; char packedpath[MAX_PATHNAME]; extern char *notename[]; -char *programname = "$VER: LoadTracker v1.99"; +const char *programname = "$VER: LoadTracker v1.99"; char specialnotenames[186]; char scalatuningfilepath[MAX_PATHNAME]; char tuningname[64]; @@ -102,7 +116,7 @@ extern unsigned char datafile[]; COLORS colors; -char* usage[] = { +const char* usage[] = { "Usage: loadtrk [songname] [options]", "Options:", "-Axx Set ADSR parameter for hardrestart in hex. DEFAULT=0F00", @@ -136,13 +150,32 @@ char* usage[] = { int usagelen = (sizeof usage / sizeof usage[0]); +void converthex(); +void docommand(); +void mousecommands(); +void generalcommands(); +void load(); +void save(); +void quit(); +void clear(); +void getparam(FILE *handle, unsigned *value); +void getfloatparam(FILE *handle, float *value); +void getstringparam(FILE *handle, char *value); +void prevmultiplier(); +void nextmultiplier(); +void editadsr(); +void editbpm(); +void readscalatuningfile(); +void setspecialnotenames(); +void calculatefreqtable(); +void switchMode(); + int main(int argc, char **argv) { char filename[MAX_PATHNAME]; FILE *configfile; - int c,d; - int dark = 0; + bool dark = false; programname += sizeof "$VER:"; // Open datafile @@ -155,18 +188,18 @@ int main(int argc, char **argv) filename[strlen(filename)-2] = 'f'; filename[strlen(filename)-1] = 'g'; #elif __amigaos__ - strcpy(filename, "PROGDIR:loadtrk.cfg"); + std::strcpy(filename, "PROGDIR:loadtrk.cfg"); #else char* xdg_home = getenv("XDG_CONFIG_HOME"); if (xdg_home) { - strcpy(filename, xdg_home); - strcat(filename, "/loadtrk/loadtrk.cfg"); + std::strcpy(filename, xdg_home); + std::strcat(filename, "/loadtrk/loadtrk.cfg"); } else { - strcpy(filename, getenv("HOME")); - strcat(filename, "/.config/loadtrk/loadtrk.cfg"); + std::strcpy(filename, getenv("HOME")); + std::strcat(filename, "/.config/loadtrk/loadtrk.cfg"); } #endif specialnotenames[0] = 0; @@ -217,7 +250,7 @@ int main(int argc, char **argv) initpaths(); // Scan command line - for (c = 1; c < argc; c++) + for (int c = 1; c < argc; c++) { #ifdef __WIN32__ if ((argv[c][0] == '-') || (argv[c][0] == '/')) @@ -225,16 +258,15 @@ int main(int argc, char **argv) if (argv[c][0] == '-') #endif { - int y; switch (argv[c][1]) //switch (toupper(argv[c][1])) { case '-': - if (strcmp(argv[c], "--dark") == 0) + if (std::strcmp(argv[c], "--dark") == 0) { - dark = 1; + dark = true; break; } - if (strcmp(argv[c], "--help")) + if (std::strcmp(argv[c], "--help")) break; /* fall through */ case '?': @@ -248,45 +280,45 @@ int main(int argc, char **argv) #ifdef __WIN32__ if (!initscreen()) return EXIT_FAILURE; - for (y = 0; y < usagelen; ++y) + for (int y = 0; y < usagelen; ++y) printtext(0,y,15,usage[y]); waitkeynoupdate(); #else - for (y = 0; y < usagelen; ++y) + for (int y = 0; y < usagelen; ++y) printf("%s\n", usage[y]); #endif return EXIT_SUCCESS; case 'Z': - sscanf(&argv[c][2], "%u", &residdelay); + std::sscanf(&argv[c][2], "%u", &residdelay); break; case 'A': - sscanf(&argv[c][2], "%x", &adparam); + std::sscanf(&argv[c][2], "%x", &adparam); break; case 'S': - sscanf(&argv[c][2], "%u", &multiplier); + std::sscanf(&argv[c][2], "%u", &multiplier); break; case 'D': - sscanf(&argv[c][2], "%u", &patterndispmode); + std::sscanf(&argv[c][2], "%u", &patterndispmode); break; case 'E': - sscanf(&argv[c][2], "%u", &sidmodel); + std::sscanf(&argv[c][2], "%u", &sidmodel); break; case 'I': - sscanf(&argv[c][2], "%u", &interpolate); + std::sscanf(&argv[c][2], "%u", &interpolate); break; case 'K': - sscanf(&argv[c][2], "%u", &keypreset); + std::sscanf(&argv[c][2], "%u", &keypreset); break; case 'L': - sscanf(&argv[c][2], "%x", &sidaddress); + std::sscanf(&argv[c][2], "%x", &sidaddress); break; case 'N': @@ -300,23 +332,23 @@ int main(int argc, char **argv) break; case 'F': - sscanf(&argv[c][2], "%u", &customclockrate); + std::sscanf(&argv[c][2], "%u", &customclockrate); break; case 'M': - sscanf(&argv[c][2], "%u", &mr); + std::sscanf(&argv[c][2], "%u", &mr); break; case 'O': - sscanf(&argv[c][2], "%u", &optimizepulse); + std::sscanf(&argv[c][2], "%u", &optimizepulse); break; case 'R': - sscanf(&argv[c][2], "%u", &optimizerealtime); + std::sscanf(&argv[c][2], "%u", &optimizerealtime); break; case 'V': - sscanf(&argv[c][2], "%u", &finevibrato); + std::sscanf(&argv[c][2], "%u", &finevibrato); break; case 'W': @@ -324,35 +356,35 @@ int main(int argc, char **argv) break; case 'X': - sscanf(&argv[c][2], "%d", &win_fullscreen); + std::sscanf(&argv[c][2], "%d", &win_fullscreen); break; case 'G': - sscanf(&argv[c][2], "%f", &basepitch); + std::sscanf(&argv[c][2], "%f", &basepitch); break; case 'b': - sscanf(&argv[c][2], "%f", &filterbias); + std::sscanf(&argv[c][2], "%f", &filterbias); break; case 'c': - sscanf(&argv[c][2], "%u", &combwaves); + std::sscanf(&argv[c][2], "%u", &combwaves); break; case 'Q': - sscanf(&argv[c][2], "%f", &equaldivisionsperoctave); + std::sscanf(&argv[c][2], "%f", &equaldivisionsperoctave); break; case 'J': - sscanf(&argv[c][2], "%s", specialnotenames); + std::sscanf(&argv[c][2], "%s", specialnotenames); break; case 'Y': - sscanf(&argv[c][2], "%s", scalatuningfilepath); + std::sscanf(&argv[c][2], "%s", scalatuningfilepath); break; case 'x': - sscanf(&argv[c][2], "%u", &exsid); + std::sscanf(&argv[c][2], "%u", &exsid); break; } } @@ -360,16 +392,16 @@ int main(int argc, char **argv) { char startpath[MAX_PATHNAME]; - strcpy(songfilename, argv[c]); - for (d = strlen(argv[c])-1; d >= 0; d--) + std::strcpy(songfilename, argv[c]); + for (int d = strlen(argv[c])-1; d >= 0; d--) { if ((argv[c][d] == '/') || (argv[c][d] == '\\')) { - strcpy(startpath, argv[c]); + std::strcpy(startpath, argv[c]); startpath[d+1] = 0; chdir(startpath); initpaths(); - strcpy(songfilename, &argv[c][d+1]); + std::strcpy(songfilename, &argv[c][d+1]); break; } } @@ -439,7 +471,7 @@ int main(int argc, char **argv) } // Load song if applicable - if (strlen(songfilename)) loadsong(); + if (std::strlen(songfilename)) loadsong(); // Start editor mainloop printmainscreen(); @@ -452,26 +484,28 @@ int main(int argc, char **argv) // Shutdown sound output now sound_uninit(); + io_closelinkeddatafile(); + // Save configuration - #ifndef __WIN32__ - #ifdef __amigaos__ - strcpy(filename, "PROGDIR:loadtrk.cfg"); - #else +#ifndef __WIN32__ +# ifdef __amigaos__ + std::strcpy(filename, "PROGDIR:loadtrk.cfg"); +# else xdg_home = getenv("XDG_CONFIG_HOME"); if (xdg_home) { - strcpy(filename, xdg_home); - strcat(filename, "/loadtrk"); + std::strcpy(filename, xdg_home); + std::strcat(filename, "/loadtrk"); } else { - strcpy(filename, getenv("HOME")); - strcat(filename, "/.config/loadtrk"); + std::strcpy(filename, getenv("HOME")); + std::strcat(filename, "/.config/loadtrk"); } mkdir(filename, S_IRUSR | S_IWUSR | S_IXUSR); - strcat(filename, "/loadtrk.cfg"); - #endif - #endif + std::strcat(filename, "/loadtrk.cfg"); +# endif +#endif configfile = fopen(filename, "wt"); if (configfile) { @@ -551,7 +585,7 @@ int main(int argc, char **argv) return EXIT_SUCCESS; } -void waitkey(void) +void waitkey() { for (;;) { @@ -564,7 +598,7 @@ void waitkey(void) converthex(); } -void waitkeymouse(void) +void waitkeymouse() { for (;;) { @@ -578,7 +612,7 @@ void waitkeymouse(void) converthex(); } -void waitkeymousenoupdate(void) +void waitkeymousenoupdate() { for (;;) { @@ -592,11 +626,11 @@ void waitkeymousenoupdate(void) converthex(); } -void waitkeynoupdate(void) +void waitkeynoupdate() { for (;;) { - fliptoscreen(); + fliptoscreen(); getkey(); if ((rawkey) || (key)) break; if ((mouseb) && (!prevmouseb)) break; @@ -611,7 +645,7 @@ void converthex() hexnybble = -1; for (c = 0; c < 16; c++) { - if (tolower(key) == hexkeytbl[c]) + if (std::tolower(key) == hexkeytbl[c]) { if (c >= 10) { @@ -625,8 +659,7 @@ void converthex() } } - -void docommand(void) +void docommand() { // "GUI" operation :) mousecommands(); @@ -666,9 +699,8 @@ void docommand(void) generalcommands(); } -void mousecommands(void) +void mousecommands() { - int c; int maxChns = MAX_CHN; if (numsids == 1) maxChns = 3; @@ -699,7 +731,7 @@ void mousecommands(void) if (!mouseb) return; // Pattern editpos & pattern number selection - for (c = 0; c < maxChns; c++) + for (int c = 0; c < maxChns; c++) { if ((mousey == dpos.patternsY) && (mousex >= dpos.patternsX + 10 + c*13) && @@ -859,7 +891,7 @@ void mousecommands(void) } // Table editpos - for (c = 0; c < MAX_TABLES; c++) + for (int c = 0; c < MAX_TABLES; c++) { if ((mousey >= dpos.instrumentsY+7) && (mousey <= dpos.instrumentsY+8+VISIBLETABLEROWS) && @@ -917,7 +949,7 @@ void mousecommands(void) { recordmode ^= 1; } - for (c = 0; c < maxChns; c++) + for (int c = 0; c < maxChns; c++) { if ((!prevmouseb) && (mousey >= dpos.channelsY) && @@ -1011,9 +1043,8 @@ void mousecommands(void) } } -void generalcommands(void) +void generalcommands() { - int c; int maxChns = MAX_CHN; if (numsids == 1) maxChns = 3; int visibleOrderlist = 14; @@ -1070,7 +1101,7 @@ void generalcommands(void) break; case ';': - for (c = 0; c < maxChns; c++) + for (int c = 0; c < maxChns; c++) { if (espos[c]) espos[c]--; if (espos[c] < esview) @@ -1088,7 +1119,7 @@ void generalcommands(void) { visibleOrderlist = VISIBLEORDERLIST; } - for (c = 0; c < maxChns; c++) + for (int c = 0; c < maxChns; c++) { if (numsids == 1) { @@ -1250,7 +1281,7 @@ void generalcommands(void) } } -void load(void) +void load() { if ((editmode != EDIT_INSTRUMENT) && (editmode != EDIT_TABLES)) { @@ -1277,7 +1308,7 @@ void load(void) rawkey = 0; } -void save(void) +void save() { if ((editmode != EDIT_INSTRUMENT) && (editmode != EDIT_TABLES)) { @@ -1327,7 +1358,7 @@ void save(void) rawkey = 0; } -void quit(void) +void quit() { if ((!shiftpressed) || (mouseb)) { @@ -1340,7 +1371,7 @@ void quit(void) rawkey = 0; } -void clear(void) +void clear() { int cs = 0; int cp = 0; @@ -1441,7 +1472,7 @@ void clear(void) rawkey = 0; } -void editadsr(void) +void editadsr() { eamode = 1; eacolumn = 0; @@ -1518,7 +1549,7 @@ void editadsr(void) } } -void editbpm(void) +void editbpm() { eamode = 1; ebmode = 1; @@ -1561,7 +1592,7 @@ void editbpm(void) { case KEY_F7: if (!shiftpressed) break; - + // fall through case KEY_ESC: case KEY_ENTER: case KEY_TAB: @@ -1573,6 +1604,7 @@ void editbpm(void) case KEY_BACKSPACE: if (!eacolumn) break; + // fall through case KEY_LEFT: eacolumn--; break; @@ -1594,23 +1626,21 @@ void editbpm(void) void getparam(FILE *handle, unsigned *value) { - char *configptr; - for (;;) { if (feof(handle)) return; - fgets(configbuf, MAX_PATHNAME, handle); + std::fgets(configbuf, MAX_PATHNAME, handle); if ((configbuf[0]) && (configbuf[0] != ';') && (configbuf[0] != ' ') && (configbuf[0] != 13) && (configbuf[0] != 10)) break; } - configptr = configbuf; + char *configptr = configbuf; if (*configptr == '$') { *value = 0; configptr++; for (;;) { - char c = tolower(*configptr++); + char c = std::tolower(*configptr++); int h = -1; if ((c >= 'a') && (c <= 'f')) h = c - 'a' + 10; @@ -1629,7 +1659,7 @@ void getparam(FILE *handle, unsigned *value) *value = 0; for (;;) { - char c = tolower(*configptr++); + char c = std::tolower(*configptr++); int d = -1; if ((c >= '0') && (c <= '9')) d = c - '0'; @@ -1646,37 +1676,33 @@ void getparam(FILE *handle, unsigned *value) void getfloatparam(FILE *handle, float *value) { - char *configptr; - for (;;) { if (feof(handle)) return; - fgets(configbuf, MAX_PATHNAME, handle); + std::fgets(configbuf, MAX_PATHNAME, handle); if ((configbuf[0]) && (configbuf[0] != ';') && (configbuf[0] != ' ') && (configbuf[0] != 13) && (configbuf[0] != 10)) break; } - configptr = configbuf; + char *configptr = configbuf; *value = 0.0f; - sscanf(configptr, "%f", value); + std::sscanf(configptr, "%f", value); } void getstringparam(FILE *handle, char *value) { - char *configptr; - for (;;) { if (feof(handle)) return; - fgets(configbuf, MAX_PATHNAME, handle); + std::fgets(configbuf, MAX_PATHNAME, handle); if ((configbuf[0]) && (configbuf[0] != ';') && (configbuf[0] != ' ') && (configbuf[0] != 13) && (configbuf[0] != 10)) break; } - configptr = configbuf; + char *configptr = configbuf; - sscanf(configptr, "%s", value); + std::sscanf(configptr, "%s", value); } -void prevmultiplier(void) +void prevmultiplier() { if (multiplier > 0) { @@ -1685,7 +1711,7 @@ void prevmultiplier(void) } } -void nextmultiplier(void) +void nextmultiplier() { if (multiplier < 16) { @@ -1696,18 +1722,16 @@ void nextmultiplier(void) void calculatefreqtable() { - double basefreq = (double)basepitch * (16777216.0 / 985248.0) * pow(2.0, 0.25) / 32.0; + double basefreq = (double)basepitch * (16777216.0 / 985248.0) * std::pow(2.0, 0.25) / 32.0; double cyclebasefreq = basefreq; double freq = basefreq; - int c; - int i; if (tuningcount) { - c = 0; + int c = 0; while (c < 96) { - for (i = 0; i < tuningcount; i++) + for (int i = 0; i < tuningcount; i++) { if (c < 96) { @@ -1725,7 +1749,7 @@ void calculatefreqtable() } else { - for (c = 0; c < 8*12 ; c++) + for (int c = 0; c < 8*12 ; c++) { double note = c; double freq = basefreq * pow(2.0, note/(double)equaldivisionsperoctave); @@ -1740,26 +1764,22 @@ void calculatefreqtable() void setspecialnotenames() { - int i; - int j; - int oct; - char *name; char octave[11]; - - i = 0; - oct = 0; + + int i = 0; + int oct = 0; while (i < 93) { - for (j = 0; j < 186; j += 2) + for (int j = 0; j < 186; j += 2) { if (specialnotenames[j] == '\0') break; if (i < 93) { - name = malloc(4); - strncpy(name, specialnotenames + j, 2); + char *name = (char*)std::malloc(4); + std::strncpy(name, specialnotenames + j, 2); sprintf(octave, "%d", oct); - strcpy(name + 2, octave); + std::strcpy(name + 2, octave); notename[i] = name; i++; } @@ -1770,62 +1790,60 @@ void setspecialnotenames() void readscalatuningfile() { - FILE *scalatuningfile; char *configptr; char strbuf[64]; char name[3]; - int i; double numerator; double denominator; double centvalue; - scalatuningfile = fopen(scalatuningfilepath, "rt"); + FILE *scalatuningfile = fopen(scalatuningfilepath, "rt"); if (scalatuningfile) { // Tuning name for (;;) { if (feof(scalatuningfile)) return; - fgets(configbuf, MAX_PATHNAME, scalatuningfile); + std::fgets(configbuf, MAX_PATHNAME, scalatuningfile); if ((configbuf[0]) && (configbuf[0] != '!') && (configbuf[0] != 13) && (configbuf[0] != 10)) break; } configptr = configbuf; - sscanf(configptr, "%63[^\t\n]", tuningname); + std::sscanf(configptr, "%63[^\t\n]", tuningname); // Tuning count for (;;) { if (feof(scalatuningfile)) return; - fgets(configbuf, MAX_PATHNAME, scalatuningfile); + std::fgets(configbuf, MAX_PATHNAME, scalatuningfile); if ((configbuf[0]) && (configbuf[0] != '!') && (configbuf[0] != 13) && (configbuf[0] != 10)) break; } configptr = configbuf; - sscanf(configptr, "%d", &tuningcount); + std::sscanf(configptr, "%d", &tuningcount); // Tunings - for (i = 0; i < tuningcount; i++) + for (int i = 0; i < tuningcount; i++) { for (;;) { if (feof(scalatuningfile)) return; - fgets(configbuf, MAX_PATHNAME, scalatuningfile); + std::fgets(configbuf, MAX_PATHNAME, scalatuningfile); if ((configbuf[0]) && (configbuf[0] != '!') && (configbuf[0] != 13) && (configbuf[0] != 10)) break; } configptr = configbuf; name[0] = '\0'; - sscanf(configptr, "%63s %2s", strbuf, name); + std::sscanf(configptr, "%63s %2s", strbuf, name); if (!i) { - strcpy(specialnotenames, name); + std::strcpy(specialnotenames, name); } else { if (i == tuningcount - 1) { char *tmp = strdup(specialnotenames); - strcpy(specialnotenames, name); - strcat(specialnotenames, tmp); - free(tmp); + std::strcpy(specialnotenames, name); + std::strcat(specialnotenames, tmp); + std::free(tmp); } else { @@ -1834,35 +1852,35 @@ void readscalatuningfile() } if (!strchr(strbuf, '.')) { - sscanf(strbuf, "%lf", &numerator); + std::sscanf(strbuf, "%lf", &numerator); if (strchr(strbuf, '/')) { - sscanf(strchr(strbuf, '/') + 1, "%lf", &denominator); + std::sscanf(strchr(strbuf, '/') + 1, "%lf", &denominator); tuning[i] = numerator / denominator; } } else { - sscanf(configptr, "%lf", ¢value); - tuning[i] = pow(2.0, centvalue / 1200.0); + std::sscanf(configptr, "%lf", ¢value); + tuning[i] = std::pow(2.0, centvalue / 1200.0); } } - fclose(scalatuningfile); + std::fclose(scalatuningfile); } } -void switchMode(void) +void switchMode() { char nextMode[7]; char textbuffer[80]; if (numsids == 1) { - strcpy(nextMode, "STEREO"); + std::strcpy(nextMode, "STEREO"); } else { - strcpy(nextMode, "MONO"); + std::strcpy(nextMode, "MONO"); } sprintf(textbuffer, "Switch to %s Mode (y/n) ?", nextMode); @@ -1887,7 +1905,7 @@ void switchMode(void) if ((key == 'y') || (key == 'Y')) { - memset(songfilename, 0, sizeof songfilename); + std::memset(songfilename, 0, sizeof songfilename); numsids ^= 3; clearsong(1, 1, 1, 1, 1); diff --git a/src/loadtrk.h b/src/loadtrk.h index 82b5435..2875e39 100644 --- a/src/loadtrk.h +++ b/src/loadtrk.h @@ -1,29 +1,37 @@ +/* + * LoadTracker + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + #ifndef LOADTRK_H #define LOADTRK_H -#include -#include -#include -#include -#include -#include -#include -#include -#include "bme.h" - -#include "gcommon.h" -#include "gconsole.h" -#include "gsound.h" -#include "gsid.h" -#include "gsong.h" -#include "gplay.h" -#include "gdisplay.h" -#include "greloc.h" -#include "gfile.h" -#include "gpattern.h" -#include "gorder.h" -#include "ginstr.h" -#include "gtable.h" +#include "common.h" +#include "console.h" +#include "sound.h" +#include "sid.h" +#include "song.h" +#include "play.h" +#include "display.h" +#include "reloc.h" +#include "file.h" +#include "pattern.h" +#include "order.h" +#include "instr.h" +#include "table.h" #define EDIT_PATTERN 0 #define EDIT_ORDERLIST 1 @@ -43,7 +51,7 @@ #define PGUPDNREPEAT 8 #ifndef LOADTRK_C -extern int menu; +extern bool menu; extern int editmode; extern int recordmode; extern int followplay; @@ -82,7 +90,6 @@ extern unsigned interpolate; extern unsigned numsids; extern unsigned monomode; extern float basepitch; -extern char configbuf[MAX_PATHNAME]; extern char loadedsongfilename[MAX_FILENAME]; extern char songfilename[MAX_FILENAME]; extern char songfilter[MAX_FILENAME]; @@ -91,37 +98,16 @@ extern char instrfilename[MAX_FILENAME]; extern char instrfilter[MAX_FILENAME]; extern char instrpath[MAX_PATHNAME]; extern char packedpath[MAX_PATHNAME]; -extern char *programname; -extern char *notename[]; +extern const char *programname; +extern const char *notename[]; extern char textbuffer[MAX_PATHNAME]; -extern unsigned char hexkeytbl[16]; -extern unsigned char datafile[]; extern COLORS colors; #endif -void getparam(FILE *handle, unsigned *value); -void getfloatparam(FILE *handle, float *value); -void getstringparam(FILE *handle, char *value); -void waitkey(void); -void waitkeymouse(void); -void waitkeynoupdate(void); -void waitkeymousenoupdate(void); -void converthex(void); -void docommand(void); +void waitkey(); +void waitkeymouse(); +void waitkeynoupdate(); +void waitkeymousenoupdate(); void onlinehelp(int standalone, int context); -void mousecommands(void); -void generalcommands(void); -void load(void); -void save(void); -void quit(void); -void clear(void); -void prevmultiplier(void); -void nextmultiplier(void); -void editadsr(void); -void editbpm(void); -void calculatefreqtable(void); -void setspecialnotenames(void); -void readscalatuningfile(void); -void switchMode(void); #endif diff --git a/src/loadtrk.seq b/src/loadtrk.seq index 433e244..2db70df 100644 --- a/src/loadtrk.seq +++ b/src/loadtrk.seq @@ -4,5 +4,5 @@ player_s.s altplayer_s.s chargen.bin palette.bin -cursor.bin +cursor.png loadtrk.bmp diff --git a/src/gorder.c b/src/order.cpp similarity index 88% rename from src/gorder.c rename to src/order.cpp index f4d5b3b..27fb375 100644 --- a/src/gorder.c +++ b/src/order.cpp @@ -1,12 +1,31 @@ /* - * ============================================================================= - * orderlist & songname editor - * ============================================================================= + * LoadTracker + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#define GORDER_C +// ============================================================================= +// orderlist & songname editor +// ============================================================================= + +#define ORDER_C #include "loadtrk.h" +#include "bme_main.h" + +#include unsigned char trackcopybuffer[MAX_SONGLEN+2]; int trackcopyrows = 0; @@ -25,14 +44,13 @@ int esmarkstart; int esmarkend; int enpos; -void orderlistcommands(void); -void orderlistcommands_stereo(void); -void namecommands(void); +void orderlistcommands(); +void orderlistcommands_stereo(); +void namecommands(); -void orderlistcommands(void) +void orderlistcommands() { - int c, scrrep; - int maxChns = 3; + int maxChns = MAX_CHN_MONO; if (hexnybble >= 0) { @@ -154,7 +172,7 @@ void orderlistcommands(void) songlen[esnum][schn] = songlen[esnum][tchn]; songlen[esnum][tchn] = lentemp; - for (c = 0; c < MAX_SONGLEN+2; c++) + for (int c = 0; c < MAX_SONGLEN+2; c++) { unsigned char temp = songorder[esnum][schn][c]; songorder[esnum][schn][c] = songorder[esnum][tchn][c]; @@ -175,14 +193,14 @@ void orderlistcommands(void) if (esmarkstart <= esmarkend) { eseditpos = esmarkstart; - for (c = esmarkstart; c <= esmarkend; c++) + for (int c = esmarkstart; c <= esmarkend; c++) trackcopybuffer[d++] = songorder[esnum][eschn][c]; trackcopyrows = d; } else { eseditpos = esmarkend; - for (c = esmarkend; c <= esmarkstart; c++) + for (int c = esmarkend; c <= esmarkstart; c++) trackcopybuffer[d++] = songorder[esnum][eschn][c]; trackcopyrows = d; } @@ -192,7 +210,7 @@ void orderlistcommands(void) trackcopyrpos = songorder[esnum][eschn][songlen[esnum][eschn]+1]; } else trackcopywhole = 0; - for (c = 0; c < trackcopyrows; c++) deleteorder(); + for (int c = 0; c < trackcopyrows; c++) deleteorder(); esmarkchn = -1; } } @@ -206,13 +224,13 @@ void orderlistcommands(void) int d = 0; if (esmarkstart <= esmarkend) { - for (c = esmarkstart; c <= esmarkend; c++) + for (int c = esmarkstart; c <= esmarkend; c++) trackcopybuffer[d++] = songorder[esnum][eschn][c]; trackcopyrows = d; } else { - for (c = esmarkend; c <= esmarkstart; c++) + for (int c = esmarkend; c <= esmarkstart; c++) trackcopybuffer[d++] = songorder[esnum][eschn][c]; trackcopyrows = d; } @@ -231,15 +249,15 @@ void orderlistcommands(void) if (shiftpressed) { int oldlen = songlen[esnum][eschn]; - + if (eseditpos < songlen[esnum][eschn]) { - for (c = trackcopyrows-1; c >= 0; c--) + for (int c = trackcopyrows-1; c >= 0; c--) insertorder(trackcopybuffer[c]); } else { - for (c = 0; c < trackcopyrows; c++) + for (int c = 0; c < trackcopyrows; c++) insertorder(trackcopybuffer[c]); } if ((trackcopywhole) && (!oldlen)) @@ -269,7 +287,7 @@ void orderlistcommands(void) } else { - for (c = 0; c < maxChns; c++) + for (int c = 0; c < maxChns; c++) { if (eseditpos < songlen[esnum][c]) espos[c] = eseditpos; if (esend[c] < espos[c]) esend[c] = 0; @@ -290,14 +308,14 @@ void orderlistcommands(void) { if ((esend[eschn] != eseditpos) && (eseditpos > espos[eschn])) { - for (c = 0; c < maxChns; c++) + for (int c = 0; c < maxChns; c++) { if (eseditpos < songlen[esnum][c]) esend[c] = eseditpos; } } else { - for (c = 0; c < maxChns; c++) esend[c] = 0; + for (int c = 0; c < maxChns; c++) esend[c] = 0; } } break; @@ -312,16 +330,14 @@ void orderlistcommands(void) } else { - int c, d; - - for (c = 0; c < maxChns; c++) + for (int c = 0; c < maxChns; c++) { int start; if (eseditpos != espos[eschn]) start = eseditpos; else start = espos[c]; - for (d = start; d < songlen[esnum][c]; d++) + for (int d = start; d < songlen[esnum][c]; d++) { if (songorder[esnum][c][d] < MAX_PATT) { @@ -363,12 +379,12 @@ void orderlistcommands(void) break; case KEY_PGUP: - for (scrrep = PGUPDNREPEAT * 2; scrrep; scrrep--) + for (int scrrep = PGUPDNREPEAT * 2; scrrep; scrrep--) orderleft(); break; case KEY_PGDN: - for (scrrep = PGUPDNREPEAT * 2; scrrep; scrrep--) + for (int scrrep = PGUPDNREPEAT * 2; scrrep; scrrep--) orderright(); break; @@ -414,7 +430,6 @@ void orderlistcommands(void) void orderlistcommands_stereo(void) { - int c, scrrep; int maxChns = MAX_CHN; int visibleOrderlist = 14; @@ -538,7 +553,7 @@ void orderlistcommands_stereo(void) songlen_stereo[esnum][schn] = songlen_stereo[esnum][tchn]; songlen_stereo[esnum][tchn] = lentemp; - for (c = 0; c < MAX_SONGLEN+2; c++) + for (int c = 0; c < MAX_SONGLEN+2; c++) { unsigned char temp = songorder_stereo[esnum][schn][c]; songorder_stereo[esnum][schn][c] = songorder_stereo[esnum][tchn][c]; @@ -559,14 +574,14 @@ void orderlistcommands_stereo(void) if (esmarkstart <= esmarkend) { eseditpos = esmarkstart; - for (c = esmarkstart; c <= esmarkend; c++) + for (int c = esmarkstart; c <= esmarkend; c++) trackcopybuffer[d++] = songorder_stereo[esnum][eschn][c]; trackcopyrows = d; } else { eseditpos = esmarkend; - for (c = esmarkend; c <= esmarkstart; c++) + for (int c = esmarkend; c <= esmarkstart; c++) trackcopybuffer[d++] = songorder_stereo[esnum][eschn][c]; trackcopyrows = d; } @@ -576,7 +591,7 @@ void orderlistcommands_stereo(void) trackcopyrpos = songorder_stereo[esnum][eschn][songlen_stereo[esnum][eschn]+1]; } else trackcopywhole = 0; - for (c = 0; c < trackcopyrows; c++) deleteorder_stereo(); + for (int c = 0; c < trackcopyrows; c++) deleteorder_stereo(); esmarkchn = -1; } } @@ -590,13 +605,13 @@ void orderlistcommands_stereo(void) int d = 0; if (esmarkstart <= esmarkend) { - for (c = esmarkstart; c <= esmarkend; c++) + for (int c = esmarkstart; c <= esmarkend; c++) trackcopybuffer[d++] = songorder_stereo[esnum][eschn][c]; trackcopyrows = d; } else { - for (c = esmarkend; c <= esmarkstart; c++) + for (int c = esmarkend; c <= esmarkstart; c++) trackcopybuffer[d++] = songorder_stereo[esnum][eschn][c]; trackcopyrows = d; } @@ -618,12 +633,12 @@ void orderlistcommands_stereo(void) if (eseditpos < songlen_stereo[esnum][eschn]) { - for (c = trackcopyrows-1; c >= 0; c--) + for (int c = trackcopyrows-1; c >= 0; c--) insertorder_stereo(trackcopybuffer[c]); } else { - for (c = 0; c < trackcopyrows; c++) + for (int c = 0; c < trackcopyrows; c++) insertorder_stereo(trackcopybuffer[c]); } if ((trackcopywhole) && (!oldlen)) @@ -653,7 +668,7 @@ void orderlistcommands_stereo(void) } else { - for (c = 0; c < maxChns; c++) + for (int c = 0; c < maxChns; c++) { if (eseditpos < songlen_stereo[esnum][c]) espos[c] = eseditpos; if (esend[c] < espos[c]) esend[c] = 0; @@ -674,14 +689,14 @@ void orderlistcommands_stereo(void) { if ((esend[eschn] != eseditpos) && (eseditpos > espos[eschn])) { - for (c = 0; c < maxChns; c++) + for (int c = 0; c < maxChns; c++) { if (eseditpos < songlen_stereo[esnum][c]) esend[c] = eseditpos; } } else { - for (c = 0; c < maxChns; c++) esend[c] = 0; + for (int c = 0; c < maxChns; c++) esend[c] = 0; } } break; @@ -747,12 +762,12 @@ void orderlistcommands_stereo(void) break; case KEY_PGUP: - for (scrrep = PGUPDNREPEAT * 2; scrrep; scrrep--) + for (int scrrep = PGUPDNREPEAT * 2; scrrep; scrrep--) orderleft_stereo(); break; case KEY_PGDN: - for (scrrep = PGUPDNREPEAT * 2; scrrep; scrrep--) + for (int scrrep = PGUPDNREPEAT * 2; scrrep; scrrep--) orderright_stereo(); break; @@ -796,7 +811,7 @@ void orderlistcommands_stereo(void) } } -void namecommands(void) +void namecommands() { switch(rawkey) { @@ -831,21 +846,20 @@ void insertorder(unsigned char byte) { if ((songlen[esnum][eschn] - eseditpos)-1 >= 0) { - int len; if (songlen[esnum][eschn] < MAX_SONGLEN) { - len = songlen[esnum][eschn]+1; + int len = songlen[esnum][eschn]+1; songorder[esnum][eschn][len+1] = songorder[esnum][eschn][len]; songorder[esnum][eschn][len] = LOOPSONG; if (len) songorder[esnum][eschn][len-1] = byte; countthispattern(); } - memmove(&songorder[esnum][eschn][eseditpos+1], + std::memmove(&songorder[esnum][eschn][eseditpos+1], &songorder[esnum][eschn][eseditpos], (songlen[esnum][eschn] - eseditpos)-1); songorder[esnum][eschn][eseditpos] = byte; - len = songlen[esnum][eschn]+1; + int len = songlen[esnum][eschn]+1; if ((songorder[esnum][eschn][len] > eseditpos) && (songorder[esnum][eschn][len] < (len-2))) songorder[esnum][eschn][len]++; @@ -871,21 +885,20 @@ void insertorder_stereo(unsigned char byte) { if ((songlen_stereo[esnum][eschn] - eseditpos)-1 >= 0) { - int len; if (songlen_stereo[esnum][eschn] < MAX_SONGLEN) { - len = songlen_stereo[esnum][eschn]+1; + int len = songlen_stereo[esnum][eschn]+1; songorder_stereo[esnum][eschn][len+1] = songorder_stereo[esnum][eschn][len]; songorder_stereo[esnum][eschn][len] = LOOPSONG; if (len) songorder_stereo[esnum][eschn][len-1] = byte; countthispattern(); } - memmove(&songorder_stereo[esnum][eschn][eseditpos+1], + std::memmove(&songorder_stereo[esnum][eschn][eseditpos+1], &songorder_stereo[esnum][eschn][eseditpos], (songlen_stereo[esnum][eschn] - eseditpos)-1); songorder_stereo[esnum][eschn][eseditpos] = byte; - len = songlen_stereo[esnum][eschn]+1; + int len = songlen_stereo[esnum][eschn]+1; if ((songorder_stereo[esnum][eschn][len] > eseditpos) && (songorder_stereo[esnum][eschn][len] < (len-2))) songorder_stereo[esnum][eschn][len]++; @@ -907,12 +920,11 @@ void insertorder_stereo(unsigned char byte) } } -void deleteorder(void) +void deleteorder() { if ((songlen[esnum][eschn] - eseditpos)-1 >= 0) { - int len; - memmove(&songorder[esnum][eschn][eseditpos], + std::memmove(&songorder[esnum][eschn][eseditpos], &songorder[esnum][eschn][eseditpos+1], (songlen[esnum][eschn] - eseditpos)-1); songorder[esnum][eschn][songlen[esnum][eschn]-1] = 0x00; @@ -925,7 +937,7 @@ void deleteorder(void) countthispattern(); } if (eseditpos == songlen[esnum][eschn]) eseditpos++; - len = songlen[esnum][eschn]+1; + int len = songlen[esnum][eschn]+1; if ((songorder[esnum][eschn][len] > eseditpos) && (songorder[esnum][eschn][len] > 0)) songorder[esnum][eschn][len]--; @@ -947,12 +959,12 @@ void deleteorder(void) } } -void deleteorder_stereo(void) +void deleteorder_stereo() { if ((songlen_stereo[esnum][eschn] - eseditpos)-1 >= 0) { int len; - memmove(&songorder_stereo[esnum][eschn][eseditpos], + std::memmove(&songorder_stereo[esnum][eschn][eseditpos], &songorder_stereo[esnum][eschn][eseditpos+1], (songlen_stereo[esnum][eschn] - eseditpos)-1); songorder_stereo[esnum][eschn][songlen_stereo[esnum][eschn]-1] = 0x00; @@ -987,7 +999,7 @@ void deleteorder_stereo(void) } } -void orderleft(void) +void orderleft() { if ((shiftpressed) && (eseditpos < songlen[esnum][eschn])) { @@ -1016,7 +1028,7 @@ void orderleft(void) if ((shiftpressed) && (eseditpos < songlen[esnum][eschn])) esmarkend = eseditpos; } -void orderleft_stereo(void) +void orderleft_stereo() { if ((shiftpressed) && (eseditpos < songlen_stereo[esnum][eschn])) { @@ -1044,7 +1056,8 @@ void orderleft_stereo(void) } if ((shiftpressed) && (eseditpos < songlen_stereo[esnum][eschn])) esmarkend = eseditpos; } -void orderright(void) + +void orderright() { if ((shiftpressed) && (eseditpos < songlen[esnum][eschn])) { @@ -1068,7 +1081,7 @@ void orderright(void) if ((shiftpressed) && (eseditpos < songlen[esnum][eschn])) esmarkend = eseditpos; } -void orderright_stereo(void) +void orderright_stereo() { if ((shiftpressed) && (eseditpos < songlen_stereo[esnum][eschn])) { @@ -1092,14 +1105,14 @@ void orderright_stereo(void) if ((shiftpressed) && (eseditpos < songlen_stereo[esnum][eschn])) esmarkend = eseditpos; } -void nextsong(void) +void nextsong() { esnum++; if (esnum >= MAX_SONGS) esnum = MAX_SONGS - 1; songchange(); } -void prevsong(void) +void prevsong() { esnum--; if (esnum < 0) esnum = 0; @@ -1108,9 +1121,8 @@ void prevsong(void) void songchange(void) { - int c; int maxChns = MAX_CHN; - if (numsids == 1) maxChns = 3; + if (numsids == 1) maxChns = MAX_CHN_MONO; int currentSonglen = 0; if (numsids == 1) { @@ -1121,7 +1133,7 @@ void songchange(void) currentSonglen = songlen_stereo[esnum][eschn]; } - for (c = 0; c < maxChns; c++) + for (int c = 0; c < maxChns; c++) { espos[c] = 0; esend[c] = 0; @@ -1139,16 +1151,14 @@ void songchange(void) stopsong(); } -void updateviewtopos(void) +void updateviewtopos() { - int c, d; int maxChns = MAX_CHN; - if (numsids == 1) maxChns = 3; - int currentSonglen = 0; - int currentSongorder = 0; + if (numsids == 1) maxChns = MAX_CHN_MONO; - for (c = 0; c < maxChns; c++) + for (int c = 0; c < maxChns; c++) { + int currentSonglen = 0; if (numsids == 1) { currentSonglen = songlen[esnum][c]; @@ -1157,8 +1167,9 @@ void updateviewtopos(void) { currentSonglen = songlen_stereo[esnum][c]; } - for (d = espos[c]; d < currentSonglen; d++) + for (int d = espos[c]; d < currentSonglen; d++) { + int currentSongorder = 0; if (numsids == 1) { currentSongorder = songorder[esnum][c][d]; diff --git a/src/order.h b/src/order.h new file mode 100644 index 0000000..bf3fe41 --- /dev/null +++ b/src/order.h @@ -0,0 +1,52 @@ +/* + * LoadTracker + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef ORDER_H +#define ORDER_H + +#ifndef ORDER_C +extern int espos[MAX_CHN]; +extern int esend[MAX_CHN]; +extern int eseditpos; +extern int esview; +extern int escolumn; +extern int eschn; +extern int esnum; +extern int esmarkchn; +extern int esmarkstart; +extern int esmarkend; +extern int enpos; +#endif + +void updateviewtopos(void); +void orderlistcommands(void); +void orderlistcommands_stereo(void); +void namecommands(void); +void nextsong(void); +void prevsong(void); +void songchange(void); +void orderleft(void); +void orderright(void); +void deleteorder(void); +void insertorder(unsigned char byte); +void orderleft_stereo(void); +void orderright_stereo(void); +void deleteorder_stereo(void); +void insertorder_stereo(unsigned char byte); + +#endif diff --git a/src/gpattern.c b/src/pattern.cpp similarity index 88% rename from src/gpattern.c rename to src/pattern.cpp index 3fdbc49..a03c1b8 100644 --- a/src/gpattern.c +++ b/src/pattern.cpp @@ -1,12 +1,31 @@ /* - * ============================================================================= - * pattern editor - * ============================================================================= + * LoadTracker + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#define GPATTERN_C +// ============================================================================= +// pattern editor +// ============================================================================= + +#define PATTERN_C #include "loadtrk.h" +#include "bme_main.h" + +#include unsigned char notekeytbl1[] = {KEY_Z, KEY_S, KEY_X, KEY_D, KEY_C, KEY_V, KEY_G, KEY_B, KEY_H, KEY_N, KEY_J, KEY_M, KEY_COMMA, KEY_L, KEY_COLON}; @@ -38,7 +57,13 @@ int epmarkchn = -1; int epmarkstart; int epmarkend; -void insertnote(int newnote) { +void shrinkpattern(); +void expandpattern(); +void splitpattern(); +void joinpattern(); + +void insertnote(int newnote) +{ if ((recordmode) && (eppos < pattlen[epnum[epchn]])) { pattern[epnum[epchn]][eppos*4] = newnote; @@ -71,12 +96,10 @@ void insertnote(int newnote) { playtestnote(newnote, einum, epchn); } -void patterncommands(void) +void patterncommands() { - size_t i; - int c, scrrep; int maxChns = MAX_CHN; - if (numsids == 1) maxChns = 3; + if (numsids == 1) maxChns = MAX_CHN_MONO; switch(key) { @@ -99,14 +122,14 @@ void patterncommands(void) switch (keypreset) { case KEY_TRACKER: - for (i = 0; i < sizeof(notekeytbl1); i++) + for (size_t i = 0; i < sizeof(notekeytbl1); i++) { if ((rawkey == notekeytbl1[i]) && (!epcolumn) && (!shiftpressed) && (!altpressed)) { newnote = FIRSTNOTE+i+epoctave*12; } } - for (i = 0; i < sizeof(notekeytbl2); i++) + for (size_t i = 0; i < sizeof(notekeytbl2); i++) { if ((rawkey == notekeytbl2[i]) && (!epcolumn) && (!shiftpressed) && (!altpressed)) { @@ -116,7 +139,7 @@ void patterncommands(void) break; case KEY_DMC: - for (i = 0; i < sizeof(dmckeytbl); i++) + for (size_t i = 0; i < sizeof(dmckeytbl); i++) { if ((rawkey == dmckeytbl[i]) && (!epcolumn) && (!shiftpressed) && (!altpressed)) { @@ -126,14 +149,14 @@ void patterncommands(void) break; case KEY_JANKO: - for (i = 0; i < sizeof(jankokeytbl1); i++) + for (size_t i = 0; i < sizeof(jankokeytbl1); i++) { if ((rawkey == jankokeytbl1[i]) && (!epcolumn) && (!shiftpressed) && (!altpressed)) { newnote = FIRSTNOTE+i+epoctave*12; } } - for (i = 0; i < sizeof(jankokeytbl2); i++) + for (size_t i = 0; i < sizeof(jankokeytbl2); i++) { if ((rawkey == jankokeytbl2[i]) && (!epcolumn) && (!shiftpressed) && (!altpressed)) { @@ -370,7 +393,7 @@ void patterncommands(void) if (epmarkstart < epmarkend) { int d = 0; - for (c = epmarkstart; c <= epmarkend; c++) + for (int c = epmarkstart; c <= epmarkend; c++) { if (c >= pattlen[epnum[epmarkchn]]) break; cmdcopybuffer[d*4+2] = pattern[epnum[epmarkchn]][c*4+2]; @@ -382,7 +405,7 @@ void patterncommands(void) else { int d = 0; - for (c = epmarkend; c <= epmarkstart; c++) + for (int c = epmarkend; c <= epmarkstart; c++) { if (c >= pattlen[epnum[epmarkchn]]) break; cmdcopybuffer[d*4+2] = pattern[epnum[epmarkchn]][c*4+2]; @@ -408,7 +431,7 @@ void patterncommands(void) case KEY_R: if (shiftpressed) { - for (c = 0; c < cmdcopyrows; c++) + for (int c = 0; c < cmdcopyrows; c++) { if (eppos >= pattlen[epnum[epchn]]) break; pattern[epnum[epchn]][eppos*4+2] = cmdcopybuffer[c*4+2]; @@ -421,17 +444,16 @@ void patterncommands(void) case KEY_I: if (shiftpressed) { - int d, e; char temp; if (epmarkchn != -1) { if (epmarkstart <= epmarkend) { - e = epmarkend; - for (c = epmarkstart; c <= epmarkend; c++) + int e = epmarkend; + for (int c = epmarkstart; c <= epmarkend; c++) { if (c >= pattlen[epnum[epmarkchn]]) break; - for (d = 0; d < 4; d++) + for (int d = 0; d < 4; d++) { temp = pattern[epnum[epmarkchn]][c*4+d]; pattern[epnum[epmarkchn]][c*4+d] = pattern[epnum[epmarkchn]][e*4+d]; @@ -443,11 +465,11 @@ void patterncommands(void) } else { - e = epmarkstart; - for (c = epmarkend; c <= epmarkstart; c++) + int e = epmarkstart; + for (int c = epmarkend; c <= epmarkstart; c++) { if (c >= pattlen[epnum[epmarkchn]]) break; - for (d = 0; d < 4; d++) + for (int d = 0; d < 4; d++) { temp = pattern[epnum[epmarkchn]][c*4+d]; pattern[epnum[epmarkchn]][c*4+d] = pattern[epnum[epmarkchn]][e*4+d]; @@ -460,10 +482,10 @@ void patterncommands(void) } else { - e = pattlen[epnum[epchn]] - 1; - for (c = 0; c < pattlen[epnum[epchn]]; c++) + int e = pattlen[epnum[epchn]] - 1; + for (int c = 0; c < pattlen[epnum[epchn]]; c++) { - for (d = 0; d < 4; d++) + for (int d = 0; d < 4; d++) { temp = pattern[epnum[epchn]][c*4+d]; pattern[epnum[epchn]][c*4+d] = pattern[epnum[epchn]][e*4+d]; @@ -483,7 +505,7 @@ void patterncommands(void) { if (epmarkstart <= epmarkend) { - for (c = epmarkstart; c <= epmarkend; c++) + for (int c = epmarkstart; c <= epmarkend; c++) { if (c >= pattlen[epnum[epmarkchn]]) break; if ((pattern[epnum[epmarkchn]][c*4] < LASTNOTE) && @@ -493,7 +515,7 @@ void patterncommands(void) } else { - for (c = epmarkend; c <= epmarkstart; c++) + for (int c = epmarkend; c <= epmarkstart; c++) { if (c >= pattlen[epnum[epmarkchn]]) break; if ((pattern[epnum[epmarkchn]][c*4] < LASTNOTE) && @@ -504,7 +526,7 @@ void patterncommands(void) } else { - for (c = 0; c < pattlen[epnum[epchn]]; c++) + for (int c = 0; c < pattlen[epnum[epchn]]; c++) { if ((pattern[epnum[epchn]][c*4] < LASTNOTE) && (pattern[epnum[epchn]][c*4] >= FIRSTNOTE)) @@ -521,7 +543,7 @@ void patterncommands(void) { if (epmarkstart <= epmarkend) { - for (c = epmarkstart; c <= epmarkend; c++) + for (int c = epmarkstart; c <= epmarkend; c++) { if (c >= pattlen[epnum[epmarkchn]]) break; if ((pattern[epnum[epmarkchn]][c*4] <= LASTNOTE) && @@ -531,7 +553,7 @@ void patterncommands(void) } else { - for (c = epmarkend; c <= epmarkstart; c++) + for (int c = epmarkend; c <= epmarkstart; c++) { if (c >= pattlen[epnum[epmarkchn]]) break; if ((pattern[epnum[epmarkchn]][c*4] <= LASTNOTE) && @@ -542,7 +564,7 @@ void patterncommands(void) } else { - for (c = 0; c < pattlen[epnum[epchn]]; c++) + for (int c = 0; c < pattlen[epnum[epchn]]; c++) { if ((pattern[epnum[epchn]][c*4] <= LASTNOTE) && (pattern[epnum[epchn]][c*4] > FIRSTNOTE)) @@ -559,7 +581,7 @@ void patterncommands(void) { if (epmarkstart <= epmarkend) { - for (c = epmarkstart; c <= epmarkend; c++) + for (int c = epmarkstart; c <= epmarkend; c++) { if (c >= pattlen[epnum[epmarkchn]]) break; if ((pattern[epnum[epmarkchn]][c*4] <= LASTNOTE) && @@ -573,7 +595,7 @@ void patterncommands(void) } else { - for (c = epmarkend; c <= epmarkstart; c++) + for (int c = epmarkend; c <= epmarkstart; c++) { if (c >= pattlen[epnum[epmarkchn]]) break; if ((pattern[epnum[epmarkchn]][c*4] <= LASTNOTE) && @@ -588,7 +610,7 @@ void patterncommands(void) } else { - for (c = 0; c < pattlen[epnum[epchn]]; c++) + for (int c = 0; c < pattlen[epnum[epchn]]; c++) { if ((pattern[epnum[epchn]][c*4] <= LASTNOTE) && (pattern[epnum[epchn]][c*4] >= FIRSTNOTE)) @@ -609,7 +631,7 @@ void patterncommands(void) { if (epmarkstart <= epmarkend) { - for (c = epmarkstart; c <= epmarkend; c++) + for (int c = epmarkstart; c <= epmarkend; c++) { if (c >= pattlen[epnum[epmarkchn]]) break; if ((pattern[epnum[epmarkchn]][c*4] <= LASTNOTE) && @@ -623,7 +645,7 @@ void patterncommands(void) } else { - for (c = epmarkend; c <= epmarkstart; c++) + for (int c = epmarkend; c <= epmarkstart; c++) { if (c >= pattlen[epnum[epmarkchn]]) break; if ((pattern[epnum[epmarkchn]][c*4] <= LASTNOTE) && @@ -638,7 +660,7 @@ void patterncommands(void) } else { - for (c = 0; c < pattlen[epnum[epchn]]; c++) + for (int c = 0; c < pattlen[epnum[epchn]]; c++) { if ((pattern[epnum[epchn]][c*4] <= LASTNOTE) && (pattern[epnum[epchn]][c*4] >= FIRSTNOTE)) @@ -671,6 +693,7 @@ void patterncommands(void) case KEY_H: if (shiftpressed) { + int c; switch (pattern[epnum[epchn]][eppos*4+2]) { case CMD_PORTAUP: @@ -686,18 +709,14 @@ void patterncommands(void) if ((pattern[epnum[epchn]][c*4] >= FIRSTNOTE) && (pattern[epnum[epchn]][c*4] <= LASTNOTE)) { - int delta; - int pitch1; - int pitch2; - int pos; int note = pattern[epnum[epchn]][c*4] - FIRSTNOTE; int right = pattern[epnum[epchn]][eppos*4+3] & 0xf; int left = pattern[epnum[epchn]][eppos*4+3] >> 4; if (note > MAX_NOTES-1) note--; - pitch1 = freqtbllo[note] | (freqtblhi[note] << 8); - pitch2 = freqtbllo[note+1] | (freqtblhi[note+1] << 8); - delta = pitch2 - pitch1; + int pitch1 = freqtbllo[note] | (freqtblhi[note] << 8); + int pitch2 = freqtbllo[note+1] | (freqtblhi[note+1] << 8); + int delta = pitch2 - pitch1; while (left--) delta <<= 1; while (right--) delta >>= 1; @@ -706,7 +725,7 @@ void patterncommands(void) { if (delta > 0xff) delta = 0xff; } - pos = makespeedtable(delta, MST_RAW, 1); + int pos = makespeedtable(delta, MST_RAW, 1); pattern[epnum[epchn]][eppos*4+3] = pos + 1; break; } @@ -738,7 +757,7 @@ void patterncommands(void) if (epmarkstart <= epmarkend) { int d = 0; - for (c = epmarkstart; c <= epmarkend; c++) + for (int c = epmarkstart; c <= epmarkend; c++) { if (c >= pattlen[epnum[epmarkchn]]) break; patterncopybuffer[d*4] = pattern[epnum[epmarkchn]][c*4]; @@ -759,7 +778,7 @@ void patterncommands(void) else { int d = 0; - for (c = epmarkend; c <= epmarkstart; c++) + for (int c = epmarkend; c <= epmarkstart; c++) { if (c >= pattlen[epnum[epmarkchn]]) break; patterncopybuffer[d*4] = pattern[epnum[epmarkchn]][c*4]; @@ -782,7 +801,7 @@ void patterncommands(void) else { int d = 0; - for (c = 0; c < pattlen[epnum[epchn]]; c++) + for (int c = 0; c < pattlen[epnum[epchn]]; c++) { patterncopybuffer[d*4] = pattern[epnum[epchn]][c*4]; patterncopybuffer[d*4+1] = pattern[epnum[epchn]][c*4+1]; @@ -805,7 +824,7 @@ void patterncommands(void) case KEY_V: if ((shiftpressed) && (patterncopyrows)) { - for (c = 0; c < patterncopyrows; c++) + for (int c = 0; c < patterncopyrows; c++) { if (eppos >= pattlen[epnum[epchn]]) break; pattern[epnum[epchn]][eppos*4] = patterncopybuffer[c*4]; @@ -821,7 +840,7 @@ void patterncommands(void) if (epmarkchn == epchn) epmarkchn = -1; if ((pattlen[epnum[epchn]]-eppos)*4-4 >= 0) { - memmove(&pattern[epnum[epchn]][eppos*4], + std::memmove(&pattern[epnum[epchn]][eppos*4], &pattern[epnum[epchn]][eppos*4+4], (pattlen[epnum[epchn]]-eppos)*4-4); pattern[epnum[epchn]][pattlen[epnum[epchn]]*4-4] = REST; @@ -850,7 +869,7 @@ void patterncommands(void) if (epmarkchn == epchn) epmarkchn = -1; if ((pattlen[epnum[epchn]]-eppos)*4-4 >= 0) { - memmove(&pattern[epnum[epchn]][eppos*4+4], + std::memmove(&pattern[epnum[epchn]][eppos*4+4], &pattern[epnum[epchn]][eppos*4], (pattlen[epnum[epchn]]-eppos)*4-4); pattern[epnum[epchn]][eppos*4] = REST; @@ -888,9 +907,7 @@ void patterncommands(void) { if (eseditpos != espos[eschn]) { - int c; - - for (c = 0; c < maxChns; c++) + for (int c = 0; c < maxChns; c++) { if (eseditpos < songlen[esnum][c]) espos[c] = eseditpos; if (esend[c] <= espos[c]) esend[c] = 0; @@ -958,12 +975,12 @@ void patterncommands(void) break; case KEY_PGUP: - for (scrrep = PGUPDNREPEAT; scrrep; scrrep--) + for (int scrrep = PGUPDNREPEAT; scrrep; scrrep--) patternup(); break; case KEY_PGDN: - for (scrrep = PGUPDNREPEAT; scrrep; scrrep--) + for (int scrrep = PGUPDNREPEAT; scrrep; scrrep--) patterndown(); break; @@ -1085,7 +1102,7 @@ void patterncommands(void) } -void patterndown(void) +void patterndown() { if (shiftpressed) { @@ -1103,7 +1120,7 @@ void patterndown(void) if (shiftpressed) epmarkend = eppos; } -void patternup(void) +void patternup() { if (shiftpressed) { @@ -1121,7 +1138,7 @@ void patternup(void) if (shiftpressed) epmarkend = eppos; } -void prevpattern(void) +void prevpattern() { if (epnum[epchn] > 0) { @@ -1131,7 +1148,7 @@ void prevpattern(void) if (epchn == epmarkchn) epmarkchn = -1; } -void nextpattern(void) +void nextpattern() { if (epnum[epchn] < MAX_PATT-1) { @@ -1141,18 +1158,18 @@ void nextpattern(void) if (epchn == epmarkchn) epmarkchn = -1; } -void shrinkpattern(void) +void shrinkpattern() { int c = epnum[epchn]; - int l = pattlen[c]; - int nl = l/2; - int d; if (pattlen[c] < 2) return; stopsong(); - for (d = 0; d < nl; d++) + int l = pattlen[c]; + int nl = l/2; + + for (int d = 0; d < nl; d++) { pattern[c][d*4] = pattern[c][d*2*4]; pattern[c][d*4+1] = pattern[c][d*2*4+1]; @@ -1170,20 +1187,19 @@ void shrinkpattern(void) countthispattern(); } -void expandpattern(void) +void expandpattern() { int c = epnum[epchn]; int l = pattlen[c]; int nl = l*2; - int d; - unsigned char temp[MAX_PATTROWS*4+4]; - if (nl > MAX_PATTROWS) return; - memset(temp, 0, sizeof temp); stopsong(); - for (d = 0; d <= nl; d++) + unsigned char temp[MAX_PATTROWS*4+4]; + std::memset(temp, 0, sizeof temp); + + for (int d = 0; d <= nl; d++) { if (d & 1) { @@ -1201,23 +1217,21 @@ void expandpattern(void) } } - memcpy(pattern[c], temp, (nl+1)*4); + std::memcpy(pattern[c], temp, (nl+1)*4); eppos *= 2; countthispattern(); } -void splitpattern(void) +void splitpattern() { int c = epnum[epchn]; int l = pattlen[c]; - int d; int maxChns = MAX_CHN; - if (numsids == 1) maxChns = 3; + if (numsids == 1) maxChns = MAX_CHN_MONO; - if (eppos == 0) return; - if (eppos == l) return; + if ((eppos == 0) || (eppos == l)) return; stopsong(); @@ -1227,7 +1241,7 @@ void splitpattern(void) int oldeschn = eschn; int oldeseditpos = eseditpos; - for (d = eppos; d <= l; d++) + for (int d = eppos; d <= l; d++) { pattern[c+1][(d-eppos)*4] = pattern[c][d*4]; pattern[c+1][(d-eppos)*4+1] = pattern[c][d*4+1]; @@ -1261,16 +1275,16 @@ void splitpattern(void) } } -void joinpattern(void) +void joinpattern() { int c = epnum[epchn]; - int d; int maxChns = MAX_CHN; - if (numsids == 1) maxChns = 3; + if (numsids == 1) maxChns = MAX_CHN_MONO; if (eschn != epchn) return; if (songorder[esnum][epchn][eseditpos] != c) return; - d = songorder[esnum][epchn][eseditpos + 1]; + + int d = songorder[esnum][epchn][eseditpos + 1]; if (d >= MAX_PATT) return; if (pattlen[c] + pattlen[d] > MAX_PATTROWS) return; @@ -1281,9 +1295,9 @@ void joinpattern(void) int oldesnum = esnum; int oldeschn = eschn; int oldeseditpos = eseditpos; - int e, f; d++; + int e; for (e = 0; e < pattlen[c]; e++) { pattern[c+1][e*4] = pattern[c][e*4]; @@ -1291,7 +1305,7 @@ void joinpattern(void) pattern[c+1][e*4+2] = pattern[c][e*4+2]; pattern[c+1][e*4+3] = pattern[c][e*4+3]; } - for (f = 0; f < pattlen[d]; f++) + for (int f = 0; f < pattlen[d]; f++) { pattern[c+1][e*4] = pattern[d][f*4]; pattern[c+1][e*4+1] = pattern[d][f*4+1]; diff --git a/src/pattern.h b/src/pattern.h new file mode 100644 index 0000000..816bad4 --- /dev/null +++ b/src/pattern.h @@ -0,0 +1,40 @@ +/* + * LoadTracker + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef PATTERN_H +#define PATTERN_H + +#ifndef PATTERN_C +extern int epnum[MAX_CHN]; +extern int eppos; +extern int epview; +extern int epcolumn; +extern int epchn; +extern int epoctave; +extern int epmarkchn; +extern int epmarkstart; +extern int epmarkend; +#endif + +void patterncommands(); +void nextpattern(); +void prevpattern(); +void patternup(); +void patterndown(); + +#endif diff --git a/src/gplay.c b/src/play.cpp similarity index 97% rename from src/gplay.c rename to src/play.cpp index 963070e..7673f61 100644 --- a/src/gplay.c +++ b/src/play.cpp @@ -1,13 +1,31 @@ /* - * ============================================================================= - * playroutine - * ============================================================================= + * LoadTracker + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#define GPLAY_C +// ============================================================================= +// playroutine +// ============================================================================= + +#define PLAY_C #include "loadtrk.h" +#include + unsigned char freqtbllo[] = { 0x17,0x27,0x39,0x4b,0x5f,0x74,0x8a,0xa1,0xba,0xd4,0xf0,0x0e, 0x2d,0x4e,0x71,0x96,0xbe,0xe8,0x14,0x43,0x74,0xa9,0xe1,0x1c, @@ -57,16 +75,15 @@ int startpattpos = 0; void sequencer(int c, CHN *cptr); void sequencer_stereo(int c, CHN *cptr); -void initchannels(void) +void initchannels() { - int c; int maxChns = MAX_CHN; - if (numsids == 1) maxChns = 3; + if (numsids == 1) maxChns = MAX_CHN_MONO; CHN *cptr = &chn[0]; - memset(chn, 0, sizeof chn); + std::memset(chn, 0, sizeof chn); - for (c = 0; c < maxChns; c++) + for (int c = 0; c < maxChns; c++) { chn[c].trans = 0; chn[c].instr = 1; @@ -105,7 +122,7 @@ void initsongpos(int num, int mode, int pattpos) startpattpos = pattpos; } -void stopsong(void) +void stopsong() { if (songinit != PLAY_STOPPED) { @@ -113,7 +130,7 @@ void stopsong(void) } } -void rewindsong(void) +void rewindsong() { if (lastsonginit == PLAY_BEGINNING) lastsonginit = PLAY_POS; initsong(psnum, lastsonginit); @@ -165,17 +182,16 @@ void mutechannel(int chnnum) chn[chnnum].mute ^= 1; } -int isplaying(void) +int isplaying() { return (songinit != PLAY_STOPPED); } -void playroutine(void) +void playroutine() { INSTR *iptr; CHN *cptr = &chn[0]; - int c; - int maxChns = 3; + int maxChns = MAX_CHN_MONO; if (songinit == PLAY_STOP) followplay = 0; @@ -195,7 +211,7 @@ void playroutine(void) songinit = 0x01; } - for (c = 0; c < maxChns; c++) + for (int c = 0; c < maxChns; c++) { cptr->songptr = 0; cptr->command = 0; @@ -314,7 +330,7 @@ void playroutine(void) sidreg[0x17] = filterctrl; sidreg[0x18] = filtertype | masterfader; - for (c = 0; c < maxChns; c++) + for (int c = 0; c < maxChns; c++) { iptr = &instr[cptr->instr]; @@ -953,7 +969,7 @@ void playroutine(void) } } } - NEXTCHN: +NEXTCHN: if (cptr->mute) sidreg[0x4+7*c] = cptr->wave = 0x08; else @@ -1024,11 +1040,10 @@ void sequencer(int c, CHN *cptr) /* ========================================================================== */ -void playroutine_stereo(void) +void playroutine_stereo() { INSTR *iptr; CHN *cptr = &chn[0]; - int c; if (songinit == PLAY_STOP) followplay = 0; @@ -1046,14 +1061,14 @@ void playroutine_stereo(void) if ((songinit == 0x02) || (songinit == 0x03)) { - for (c = 0; c< MAX_CHN; c++) + for (int c = 0; c < MAX_CHN; c++) { if (espos[c] >= songlen_stereo[psnum][c]) songinit = 0x01; } } - for (c = 0; c < MAX_CHN; c++) + for (int c = 0; c < MAX_CHN; c++) { cptr->songptr = 0; cptr->command = 0; @@ -1225,7 +1240,7 @@ void playroutine_stereo(void) sidreg2[0x17] = filter2ctrl; sidreg2[0x18] = filter2type | masterfader; - for (c = 0; c < MAX_CHN; c++) + for (int c = 0; c < MAX_CHN; c++) { iptr = &instr[cptr->instr]; @@ -1781,6 +1796,7 @@ void playroutine_stereo(void) cptr->vibdelay--; break; } + // fall through case CMD_VIBRATO: { unsigned short speed = 0; diff --git a/src/play.h b/src/play.h new file mode 100644 index 0000000..7e1cfd8 --- /dev/null +++ b/src/play.h @@ -0,0 +1,80 @@ +/* + * LoadTracker + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef PLAY_H +#define PLAY_H + +#define PLAY_PLAYING 0x00 +#define PLAY_BEGINNING 0x01 +#define PLAY_POS 0x02 +#define PLAY_PATTERN 0x03 +#define PLAY_STOP 0x04 +#define PLAY_STOPPED 0x80 + +typedef struct +{ + unsigned char trans; + unsigned char instr; + unsigned char note; + unsigned char lastnote; + unsigned char newnote; + int pattptr; + unsigned char pattnum; + unsigned char songptr; + unsigned char repeat; + unsigned short freq; + unsigned char gate; + unsigned char wave; + unsigned short pulse; + unsigned char ptr[2]; + unsigned char pulsetime; + unsigned char wavetime; + unsigned char vibtime; + unsigned char vibdelay; + unsigned char command; + unsigned char cmddata; + unsigned char newcommand; + unsigned char newcmddata; + unsigned char tick; + unsigned char tempo; + unsigned char mute; + unsigned char advance; + unsigned char gatetimer; +} CHN; + +#ifndef PLAY_C +extern CHN chn[MAX_CHN]; +extern unsigned char masterfader; +extern unsigned char freqtbllo[]; +extern unsigned char freqtblhi[]; +extern int lastsonginit; +#endif + +void initchannels(); +void initsong(int num, int playmode); +void initsongpos(int num, int playmode, int pattpos); +void stopsong(); +void rewindsong(); +void playtestnote(int note, int ins, int chnnum); +void releasenote(int chnnum); +void mutechannel(int chnnum); +int isplaying(); +void playroutine(); +void playroutine_stereo(); + +#endif diff --git a/src/greloc.c b/src/reloc.cpp similarity index 91% rename from src/greloc.c rename to src/reloc.cpp index 425a49e..6c54038 100644 --- a/src/greloc.c +++ b/src/reloc.cpp @@ -1,16 +1,59 @@ /* - * ============================================================================= - * packer/relocator - * ============================================================================= + * LoadTracker + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#define GRELOC_C +// ============================================================================= +// packer/relocator +// ============================================================================= + +#define RELOC_C #include "loadtrk.h" +#include "bme_main.h" +#include "bme_end.h" +#include "bme_win.h" +#include "bme_io.h" +#include "bme_snd.h" + +extern "C" { #include "membuf.h" #include "parse.h" +} + +#include +#include +#include + +#define PLAYER_BUFFERED 8 +#define PLAYER_SOUNDEFFECTS 16 +#define PLAYER_VOLUME 32 +#define PLAYER_AUTHORINFO 64 +#define PLAYER_ZPGHOSTREGS 128 +#define PLAYER_NOOPTIMIZATION 256 +#define PLAYER_FULLBUFFERED 512 + +#define CAUSE_NONE 0 +#define CAUSE_PATTERN 1 +#define CAUSE_INSTRUMENT 2 +#define CAUSE_WAVECMD 3 -char *playeroptname[] = +#define MAX_BYTES_PER_ROW 16 + +const char *playeroptname[] = { "Buffered SID-writes", "Sound effect support", @@ -21,13 +64,13 @@ char *playeroptname[] = "Full SID buffering" }; -char *tableleftname[] = { +const char *tableleftname[] = { "mt_wavetbl", "mt_pulsetimetbl", "mt_filttimetbl", "mt_speedlefttbl"}; -char *tablerightname[] = { +const char *tablerightname[] = { "mt_notetbl", "mt_pulsespdtbl", "mt_filtspdtbl", @@ -93,13 +136,30 @@ int ciaval; struct membuf src = STATIC_MEMBUF_INIT; struct membuf dest = STATIC_MEMBUF_INIT; -void relocator(void) + +int testoverlap(int area1start, int area1size, int area2start, int area2size); +int packpattern(unsigned char *dest, unsigned char *src, int rows); +void findtableduplicates(int num); +int isusedandselfcontained(int num, int start); +void calcspeedtest(unsigned char pos); + +int insertfile(const char *name); +void inserttext(const char *text); +void insertdefine(const char *name, int value); +void insertlabel(const char *name); +void insertbyte(unsigned char byte); +void insertbytes(const unsigned char *bytes, int size); +void insertaddrlo(const char *name); +void insertaddrhi(const char *name); + + +void relocator() { char packedsongname[MAX_FILENAME]; char packedfilter[MAX_FILENAME]; unsigned char *packeddata = NULL; - char *playername = "player.s"; + const char *playername = "player.s"; int tableerrortype = TYPE_NONE; int tableerrorcause = CAUSE_NONE; @@ -135,10 +195,7 @@ void relocator(void) #endif unsigned char speedcode[] = {0xa2,0x00,0x8e,0x04,0xdc,0xa2,0x00,0x8e,0x05,0xdc}; -#ifndef GT2RELOC - size_t i; -#endif - int c,d,e; + int d; unsigned char patttemp[512]; unsigned char *songwork = NULL; @@ -185,12 +242,12 @@ void relocator(void) stopsong(); - memset(pattused, 0, sizeof pattused); - memset(instrused, 0, sizeof instrused); - memset(chnused, 0, sizeof chnused); - memset(chnused_stereo, 0, sizeof chnused_stereo); - memset(tableused, 0, sizeof tableused); - memset(tablemap, 0, sizeof tablemap); + std::memset(pattused, 0, sizeof pattused); + std::memset(instrused, 0, sizeof instrused); + std::memset(chnused, 0, sizeof chnused); + std::memset(chnused_stereo, 0, sizeof chnused_stereo); + std::memset(tableused, 0, sizeof tableused); + std::memset(tablemap, 0, sizeof tablemap); tableerror = 0; membuf_free(&src); @@ -201,25 +258,24 @@ void relocator(void) // Process song-orderlists countpatternlengths(); // Calculate amount of songs with nonzero length - for (c = 0; c < MAX_SONGS; c++) + for (int c = 0; c < MAX_SONGS; c++) { if ((songlen[c][0]) && (songlen[c][1]) && (songlen[c][2])) { // See which patterns are used in this song - for (d = 0; d < maxChns; d++) + for (int d = 0; d < maxChns; d++) { songdatasize += songlen[c][d]+2; - for (e = 0; e < songlen[c][d]; e++) + for (int e = 0; e < songlen[c][d]; e++) { if (songorder[c][d][e] < REPEAT) { - int f; int num = songorder[c][d][e]; pattused[num] = 1; - for (f = 0; f < pattlen[num]; f++) + for (int f = 0; f < pattlen[num]; f++) { if ((pattern[num][f*4] != REST) || (pattern[num][f*4+1]) || (pattern[num][f*4+2])) chnused[d] = 1; @@ -276,7 +332,7 @@ void relocator(void) // Build the pattern-mapping // Instrument 1 is always used instrused[1] = 1; - for (c = 0; c < MAX_PATT; c++) + for (int c = 0; c < MAX_PATT; c++) { if (pattused[c]) { @@ -343,7 +399,7 @@ void relocator(void) // Count amount of normal, nohr, and legato instruments // Also see if special first wave parameters are used - for (c = 0; c < MAX_INSTR; c++) + for (int c = 0; c < MAX_INSTR; c++) { if (instrused[c]) { @@ -362,7 +418,7 @@ void relocator(void) freelegato = freenohr + numnohr; // Build the instrument-mapping - for (c = 0; c < MAX_INSTR; c++) + for (int c = 0; c < MAX_INSTR; c++) { if (instrused[c]) { @@ -390,13 +446,13 @@ void relocator(void) } // Execute tableprograms invoked from wavetable commands - for (c = 0; c < MAX_TABLELEN; c++) + for (int c = 0; c < MAX_TABLELEN; c++) { if (tableused[WTBL][c+1]) { if ((ltable[WTBL][c] >= WAVECMD) && (ltable[WTBL][c] <= WAVELASTCMD)) { - d = -1; + int d = -1; tableerror = 0; switch(ltable[WTBL][c] - WAVECMD) @@ -413,7 +469,7 @@ void relocator(void) d = PTBL; nopulse = 0; break; - + case CMD_SETFILTERPTR: d = FTBL; nofilter = 0; @@ -444,10 +500,10 @@ void relocator(void) } // Build the table-mapping - for (c = 0; c < MAX_TABLES; c++) + for (int c = 0; c < MAX_TABLES; c++) { int e = 1; - for (d = 0; d < MAX_TABLELEN; d++) + for (int d = 0; d < MAX_TABLELEN; d++) { if (tableused[c][d+1]) { @@ -487,18 +543,18 @@ void relocator(void) switch (tableerrorsource2) { case WTBL: - strcat(textbuffer, "WAVE"); + std::strcat(textbuffer, "WAVE"); break; case PTBL: - strcat(textbuffer, "PULSE"); + std::strcat(textbuffer, "PULSE"); break; case FTBL: - strcat(textbuffer, "FILTER"); + std::strcat(textbuffer, "FILTER"); break; } - strcat(textbuffer, ")"); + std::strcat(textbuffer, ")"); break; } printtextc(MAX_ROWS/2, colors.CTITLE, textbuffer); @@ -509,7 +565,7 @@ void relocator(void) } // Find duplicate ranges in tables - for (c = 0; c < MAX_TABLES; c++) + for (int c = 0; c < MAX_TABLES; c++) findtableduplicates(c); // Select playroutine options @@ -538,7 +594,7 @@ void relocator(void) selectdone = 0; while (!selectdone) { - for (c = 0; c < MAX_OPTIONS; c++) + for (int c = 0; c < MAX_OPTIONS; c++) { int color = (opt == c) ? colors.CEDIT : colors.CNORMAL; @@ -651,7 +707,7 @@ void relocator(void) // Allocate memory for song-orderlists songtblsize = songs*6; - songwork = (unsigned char*)malloc(songdatasize); + songwork = (unsigned char*)std::malloc(songdatasize); if (!songwork) { clearscreen(); @@ -663,17 +719,18 @@ void relocator(void) // Generate songorderlists & songtable songdatasize = 0; - for (c = 0; c < songs; c++) + for (int c = 0; c < songs; c++) { if ((songlen[c][0]) && (songlen[c][1]) && (songlen[c][2])) { - for (d = 0; d < maxChns; d++) + for (int d = 0; d < maxChns; d++) { songoffset[c][d] = songdatasize; songsize[c][d] = songlen[c][d] + 2; + int e; for (e = 0; e < songlen[c][d]; e++) { // Pattern @@ -712,7 +769,7 @@ void relocator(void) } else { - for (d = 0; d < maxChns; d++) + for (int d = 0; d < maxChns; d++) { songoffset[c][d] = songdatasize; songsize[c][d] = 0; @@ -721,7 +778,7 @@ void relocator(void) } // Calculate total size of patterns - for (c = 0; c < MAX_PATT; c++) + for (int c = 0; c < MAX_PATT; c++) { if (pattused[c]) { @@ -741,7 +798,7 @@ void relocator(void) } patttblsize = patterns*2; - pattwork = (unsigned char*)malloc(pattdatasize); + pattwork = (unsigned char*)std::malloc(pattdatasize); if (!pattwork) { clearscreen(); @@ -754,7 +811,7 @@ void relocator(void) // This time pack the patterns for real pattdatasize = 0; d = 0; - for (c = 0; c < MAX_PATT; c++) + for (int c = 0; c < MAX_PATT; c++) { if (pattused[c]) { @@ -767,7 +824,7 @@ void relocator(void) // Then process instruments instrsize = instruments*9; - instrwork = (unsigned char*)malloc(instrsize); + instrwork = (unsigned char*)std::malloc(instrsize); if (!instrwork) { clearscreen(); @@ -777,7 +834,7 @@ void relocator(void) goto PRCLEANUP; } - for (c = 1; c < MAX_INSTR; c++) + for (int c = 1; c < MAX_INSTR; c++) { if (instrused[c]) { @@ -834,7 +891,7 @@ void relocator(void) if (nofilter) instrsize -= instruments; // Process tables - for (c = 0; c < MAX_TABLELEN; c++) + for (int c = 0; c < MAX_TABLELEN; c++) { if (tableused[WTBL][c+1]) { @@ -912,7 +969,7 @@ void relocator(void) } } } - for (c = 0; c < MAX_TABLELEN; c++) + for (int c = 0; c < MAX_TABLELEN; c++) { if (tableused[PTBL][c+1]) { @@ -928,7 +985,7 @@ void relocator(void) } } } - for (c = 0; c < MAX_TABLELEN; c++) + for (int c = 0; c < MAX_TABLELEN; c++) { if (tableused[FTBL][c+1]) { @@ -936,7 +993,7 @@ void relocator(void) if (ltable[FTBL][c] < 0x80) nofiltermod = 0; } } - for (c = 0; c < MAX_TABLELEN; c++) + for (int c = 0; c < MAX_TABLELEN; c++) { if (tableused[STBL][c+1]) speedtblsize += 2; } @@ -1171,18 +1228,17 @@ void relocator(void) waitkeynoupdate(); goto PRCLEANUP; } - + // Modify ghostregs to not be zeropage if needed if ((playerversion & PLAYER_FULLBUFFERED) && (playerversion & PLAYER_ZPGHOSTREGS) == 0) { int bufsize = membuf_get_size(&src); char* bufdata = (char*)membuf_get(&src); - int c; - for (c = 0; c < bufsize; c++) + for (int c = 0; c < bufsize; c++) { if (bufdata[c] == '<') { - if (memcmp(bufdata + c + 1, "ghost", 5) == 0) + if (std::memcmp(bufdata + c + 1, "ghost", 5) == 0) bufdata[c] = ' '; } } @@ -1196,13 +1252,13 @@ void relocator(void) // Insert songtable insertlabel("mt_songtbllo"); - for (c = 0; c < songs*3; c++) + for (int c = 0; c < songs*3; c++) { sprintf(textbuffer, "mt_song%d", c); insertaddrlo(textbuffer); } insertlabel("mt_songtblhi"); - for (c = 0; c < songs*3; c++) + for (int c = 0; c < songs*3; c++) { sprintf(textbuffer, "mt_song%d", c); insertaddrhi(textbuffer); @@ -1210,13 +1266,13 @@ void relocator(void) // Insert patterntable insertlabel("mt_patttbllo"); - for (c = 0; c < patterns; c++) + for (int c = 0; c < patterns; c++) { sprintf(textbuffer, "mt_patt%d", c); insertaddrlo(textbuffer); } insertlabel("mt_patttblhi"); - for (c = 0; c < patterns; c++) + for (int c = 0; c < patterns; c++) { sprintf(textbuffer, "mt_patt%d", c); insertaddrhi(textbuffer); @@ -1255,7 +1311,7 @@ void relocator(void) } // Insert tables - for (c = 0; c < MAX_TABLES; c++) + for (int c = 0; c < MAX_TABLES; c++) { if ((c == PTBL) && (nopulse)) goto SKIPTABLE; if ((c == FTBL) && (nofilter)) goto SKIPTABLE; @@ -1267,7 +1323,7 @@ void relocator(void) insertlabel(tableleftname[c]); // Table data - for (d = 0; d < MAX_TABLELEN; d++) + for (int d = 0; d < MAX_TABLELEN; d++) { if (tableused[c][d+1]) { @@ -1311,7 +1367,7 @@ void relocator(void) // Table label insertlabel(tablerightname[c]); - for (d = 0; d < MAX_TABLELEN; d++) + for (int d = 0; d < MAX_TABLELEN; d++) { if (tableused[c][d+1]) { @@ -1387,9 +1443,9 @@ void relocator(void) } // Insert orderlists - for (c = 0; c < songs; c++) + for (int c = 0; c < songs; c++) { - for (d = 0; d < maxChns; d++) + for (int d = 0; d < maxChns; d++) { sprintf(textbuffer, "mt_song%d", c*3+d); insertlabel(textbuffer); @@ -1398,7 +1454,7 @@ void relocator(void) } // Insert patterns - for (c = 0; c < patterns; c++) + for (int c = 0; c < patterns; c++) { sprintf(textbuffer, "mt_patt%d", c); insertlabel(textbuffer); @@ -1406,9 +1462,9 @@ void relocator(void) } /* { - FILE *handle = fopen("debug.s", "wb"); - fwrite(membuf_get(&src), membuf_memlen(&src), 1, handle); - fclose(handle); + FILE *handle = std::fopen("debug.s", "wb"); + std::fwrite(membuf_get(&src), membuf_memlen(&src), 1, handle); + std::fclose(handle); } */ // Assemble; on error fail in a rude way (the parser does so too) @@ -1421,7 +1477,7 @@ void relocator(void) // Copy author info if (playerversion & PLAYER_AUTHORINFO) { - for (c = 0; c < 32; c++) + for (int c = 0; c < 32; c++) { packeddata[32+c] = authorname[c]; // Convert 0 to space @@ -1472,17 +1528,17 @@ void relocator(void) { case FORMAT_SID: printtext(1, 14, colors.CEDIT, "SID - SIDPlay music file format "); - strcpy(packedfilter, "*.sid"); + std::strcpy(packedfilter, "*.sid"); break; case FORMAT_PRG: printtext(1, 14, colors.CEDIT, "PRG - C64 native format "); - strcpy(packedfilter, "*.prg"); + std::strcpy(packedfilter, "*.prg"); break; case FORMAT_BIN: printtext(1, 14, colors.CEDIT, "BIN - Raw binary format (no startaddress)"); - strcpy(packedfilter, "*.bin"); + std::strcpy(packedfilter, "*.bin"); break; } @@ -1521,8 +1577,8 @@ void relocator(void) if (selectdone == -1) goto PRCLEANUP; // By default, copy loaded song name up to the extension - memset(packedsongname, 0, sizeof packedsongname); - for (i = 0; i < strlen(loadedsongfilename); i++) + std::memset(packedsongname, 0, sizeof packedsongname); + for (size_t i = 0; i < std::strlen(loadedsongfilename); i++) { if (loadedsongfilename[i] == '.') break; packedsongname[i] = loadedsongfilename[i]; @@ -1530,15 +1586,15 @@ void relocator(void) switch (fileformat) { case FORMAT_PRG: - strcat(packedsongname, ".prg"); + std::strcat(packedsongname, ".prg"); break; case FORMAT_BIN: - strcat(packedsongname, ".bin"); + std::strcat(packedsongname, ".bin"); break; case FORMAT_SID: - strcat(packedsongname, ".sid"); + std::strcat(packedsongname, ".sid"); break; } @@ -1551,7 +1607,7 @@ void relocator(void) if (strlen(packedsongname) < MAX_FILENAME-4) { int extfound = 0; - for (c = strlen(packedsongname)-1; c >= 0; c--) + for (int c = strlen(packedsongname)-1; c >= 0; c--) { if (packedsongname[c] == '.') extfound = 1; } @@ -1560,20 +1616,20 @@ void relocator(void) switch (fileformat) { case FORMAT_PRG: - strcat(packedsongname, ".prg"); + std::strcat(packedsongname, ".prg"); break; case FORMAT_BIN: - strcat(packedsongname, ".bin"); + std::strcat(packedsongname, ".bin"); break; case FORMAT_SID: - strcat(packedsongname, ".sid"); + std::strcat(packedsongname, ".sid"); break; } } } - songhandle = fopen(packedsongname, "wb"); + songhandle = std::fopen(packedsongname, "wb"); } if (fileformat == FORMAT_PRG) @@ -1585,7 +1641,7 @@ void relocator(void) unsigned char ident[] = {'P', 'S', 'I', 'D', 0x00, 0x02, 0x00, 0x7c}; unsigned char byte; // Identification - fwrite(ident, sizeof ident, 1, songhandle); + std::fwrite(ident, sizeof ident, 1, songhandle); // Load address byte = 0x00; @@ -1649,9 +1705,9 @@ void relocator(void) fwrite8(songhandle, byte); // Songname etc. - fwrite(songname, sizeof songname, 1, songhandle); - fwrite(authorname, sizeof authorname, 1, songhandle); - fwrite(copyrightname, sizeof copyrightname, 1, songhandle); + std::fwrite(songname, sizeof songname, 1, songhandle); + std::fwrite(authorname, sizeof authorname, 1, songhandle); + std::fwrite(copyrightname, sizeof copyrightname, 1, songhandle); // Flags byte = 0x00; @@ -1684,19 +1740,19 @@ void relocator(void) byte = (playeradr) >> 8; fwrite8(songhandle, byte); } - if ((multiplier > 1) || (!multiplier)) fwrite(speedcode, 10, 1, songhandle); + if ((multiplier > 1) || (!multiplier)) std::fwrite(speedcode, 10, 1, songhandle); } - fwrite(packeddata, packedsize, 1, songhandle); - fclose(songhandle); + std::fwrite(packeddata, packedsize, 1, songhandle); + std::fclose(songhandle); PRCLEANUP: membuf_free(&src); membuf_free(&dest); - if (pattwork) free(pattwork); - if (songwork) free(songwork); - if (instrwork) free(instrwork); + if (pattwork) std::free(pattwork); + if (songwork) std::free(songwork); + if (instrwork) std::free(instrwork); printmainscreen(); key = 0; rawkey = 0; @@ -1958,7 +2014,7 @@ unsigned char swapnybbles(unsigned char n) return (lownybble << 4) | highnybble; } -int insertfile(char *name) +int insertfile(const char *name) { int size; int handle = io_open(name); @@ -2138,13 +2194,12 @@ int isusedandselfcontained(int num, int start) { int len = gettablepartlen(num, start - 1); int end = start + len - 1; - int c; // Don't use jumps only if (len == 1) return 0; // Check that whole table is used - for (c = start; c <= end; c++) + for (int c = start; c <= end; c++) { if (tableused[num][c] == 0) return 0; } @@ -2154,9 +2209,9 @@ int isusedandselfcontained(int num, int start) if ((rtable[num][end-1] < start) || (rtable[num][end-1] > end)) return 0; } // Check for jump from outside - for (c = 1; c < start; c++) + for (int c = 1; c < start; c++) if ((tableused[num][c]) && (ltable[num][c-1] == 0xff) && (rtable[num][c-1] >= start) && (rtable[num][c-1] <= end)) return 0; - for (c = end+1; c <= MAX_TABLELEN; c++) + for (int c = end+1; c <= MAX_TABLELEN; c++) if ((tableused[num][c]) && (ltable[num][c-1] == 0xff) && (rtable[num][c-1] >= start) && (rtable[num][c-1] <= end)) return 0; // OK! @@ -2176,12 +2231,12 @@ void calcspeedtest(unsigned char pos) } -void relocator_stereo(void) +void relocator_stereo() { char packedsongname[MAX_FILENAME]; char packedfilter[MAX_FILENAME]; unsigned char *packeddata = NULL; - char *playername = "player_s.s"; + const char *playername = "player_s.s"; int tableerrortype = TYPE_NONE; int tableerrorcause = CAUSE_NONE; @@ -2213,7 +2268,7 @@ void relocator_stereo(void) int selectdone; int opt = 0; unsigned char speedcode[] = {0xa2,0x00,0x8e,0x04,0xdc,0xa2,0x00,0x8e,0x05,0xdc}; - int c,d,e; + int d; unsigned char patttemp[512]; unsigned char *songwork = NULL; unsigned char *pattwork = NULL; @@ -2258,11 +2313,11 @@ void relocator_stereo(void) stopsong(); - memset(pattused, 0, sizeof pattused); - memset(instrused, 0, sizeof instrused); - memset(chnused_stereo, 0, sizeof chnused_stereo); - memset(tableused, 0, sizeof tableused); - memset(tablemap, 0, sizeof tablemap); + std::memset(pattused, 0, sizeof pattused); + std::memset(instrused, 0, sizeof instrused); + std::memset(chnused_stereo, 0, sizeof chnused_stereo); + std::memset(tableused, 0, sizeof tableused); + std::memset(tablemap, 0, sizeof tablemap); tableerror = 0; membuf_free(&src); @@ -2271,25 +2326,24 @@ void relocator_stereo(void) // Process song-orderlists countpatternlengths(); // Calculate amount of songs with nonzero length - for (c = 0; c < MAX_SONGS; c++) + for (int c = 0; c < MAX_SONGS; c++) { if ((songlen_stereo[c][0]) && (songlen_stereo[c][1]) && (songlen_stereo[c][2])) { // See which patterns are used in this song - for (d = 0; d < MAX_CHN; d++) + for (int d = 0; d < MAX_CHN; d++) { songdatasize += songlen_stereo[c][d]+2; - for (e = 0; e < songlen_stereo[c][d]; e++) + for (int e = 0; e < songlen_stereo[c][d]; e++) { if (songorder_stereo[c][d][e] < REPEAT) { - int f; int num = songorder_stereo[c][d][e]; pattused[num] = 1; - for (f = 0; f < pattlen[num]; f++) + for (int f = 0; f < pattlen[num]; f++) { if ((pattern[num][f*4] != REST) || (pattern[num][f*4+1]) || (pattern[num][f*4+2])) chnused_stereo[d] = 1; @@ -2340,7 +2394,7 @@ void relocator_stereo(void) // Build the pattern-mapping // Instrument 1 is always used instrused[1] = 1; - for (c = 0; c < MAX_PATT; c++) + for (int c = 0; c < MAX_PATT; c++) { if (pattused[c]) { @@ -2348,7 +2402,7 @@ void relocator_stereo(void) patterns++; // See which instruments/tablecommands are used - for (d = 0; d < pattlen[c]; d++) + for (int d = 0; d < pattlen[c]; d++) { tableerror = 0; @@ -2407,7 +2461,7 @@ void relocator_stereo(void) // Count amount of normal, nohr, and legato instruments // Also see if special first wave parameters are used - for (c = 0; c < MAX_INSTR; c++) + for (int c = 0; c < MAX_INSTR; c++) { if (instrused[c]) { @@ -2426,7 +2480,7 @@ void relocator_stereo(void) freelegato = freenohr + numnohr; // Build the instrument-mapping - for (c = 0; c < MAX_INSTR; c++) + for (int c = 0; c < MAX_INSTR; c++) { if (instrused[c]) { @@ -2454,13 +2508,13 @@ void relocator_stereo(void) } // Execute tableprograms invoked from wavetable commands - for (c = 0; c < MAX_TABLELEN; c++) + for (int c = 0; c < MAX_TABLELEN; c++) { if (tableused[WTBL][c+1]) { if ((ltable[WTBL][c] >= WAVECMD) && (ltable[WTBL][c] <= WAVELASTCMD)) { - d = -1; + int d = -1; tableerror = 0; switch(ltable[WTBL][c] - WAVECMD) @@ -2508,10 +2562,10 @@ void relocator_stereo(void) } // Build the table-mapping - for (c = 0; c < MAX_TABLES; c++) + for (int c = 0; c < MAX_TABLES; c++) { int e = 1; - for (d = 0; d < MAX_TABLELEN; d++) + for (int d = 0; d < MAX_TABLELEN; d++) { if (tableused[c][d+1]) { @@ -2551,18 +2605,18 @@ void relocator_stereo(void) switch (tableerrorsource2) { case WTBL: - strcat(textbuffer, "WAVE"); + std::strcat(textbuffer, "WAVE"); break; case PTBL: - strcat(textbuffer, "PULSE"); + std::strcat(textbuffer, "PULSE"); break; case FTBL: - strcat(textbuffer, "FILTER"); + std::strcat(textbuffer, "FILTER"); break; } - strcat(textbuffer, ")"); + std::strcat(textbuffer, ")"); break; } printtextc(MAX_ROWS/2, colors.CTITLE, textbuffer); @@ -2573,7 +2627,7 @@ void relocator_stereo(void) } // Find duplicate ranges in tables - for (c = 0; c < MAX_TABLES; c++) + for (int c = 0; c < MAX_TABLES; c++) findtableduplicates(c); // Select playroutine options @@ -2593,7 +2647,7 @@ void relocator_stereo(void) selectdone = 0; while (!selectdone) { - for (c = 0; c < (MAX_OPTIONS-1); c++) + for (int c = 0; c < (MAX_OPTIONS-1); c++) { int color = colors.CNORMAL; if (opt == c) color = colors.CEDIT; @@ -2705,7 +2759,7 @@ void relocator_stereo(void) // Allocate memory for song-orderlists songtblsize = songs*6; - songwork = malloc(songdatasize); + songwork = (unsigned char*)std::malloc(songdatasize); if (!songwork) { clearscreen(); @@ -2717,7 +2771,7 @@ void relocator_stereo(void) // Generate songorderlists & songtable songdatasize = 0; - for (c = 0; c < songs; c++) + for (int c = 0; c < songs; c++) { if ((songlen_stereo[c][0]) && (songlen_stereo[c][1]) && @@ -2728,6 +2782,7 @@ void relocator_stereo(void) songoffset_stereo[c][d] = songdatasize; songsize_stereo[c][d] = songlen_stereo[c][d] + 2; + int e; for (e = 0; e < songlen_stereo[c][d]; e++) { // Pattern @@ -2766,7 +2821,7 @@ void relocator_stereo(void) } else { - for (d = 0; d < MAX_CHN; d++) + for (int d = 0; d < MAX_CHN; d++) { songoffset_stereo[c][d] = songdatasize; songsize_stereo[c][d] = 0; @@ -2775,7 +2830,7 @@ void relocator_stereo(void) } // Calculate total size of patterns - for (c = 0; c < MAX_PATT; c++) + for (int c = 0; c < MAX_PATT; c++) { if (pattused[c]) { @@ -2795,7 +2850,7 @@ void relocator_stereo(void) } patttblsize = patterns*2; - pattwork = malloc(pattdatasize); + pattwork = (unsigned char*)std::malloc(pattdatasize); if (!pattwork) { clearscreen(); @@ -2808,7 +2863,7 @@ void relocator_stereo(void) // This time pack the patterns for real pattdatasize = 0; d = 0; - for (c = 0; c < MAX_PATT; c++) + for (int c = 0; c < MAX_PATT; c++) { if (pattused[c]) { @@ -2821,7 +2876,7 @@ void relocator_stereo(void) // Then process instruments instrsize = instruments*9; - instrwork = malloc(instrsize); + instrwork = (unsigned char*)std::malloc(instrsize); if (!instrwork) { clearscreen(); @@ -2831,7 +2886,7 @@ void relocator_stereo(void) goto PRCLEANUP_S; } - for (c = 1; c < MAX_INSTR; c++) + for (int c = 1; c < MAX_INSTR; c++) { if (instrused[c]) { @@ -2888,7 +2943,7 @@ void relocator_stereo(void) if (nofilter) instrsize -= instruments; // Process tables - for (c = 0; c < MAX_TABLELEN; c++) + for (int c = 0; c < MAX_TABLELEN; c++) { if (tableused[WTBL][c+1]) { @@ -2966,7 +3021,7 @@ void relocator_stereo(void) } } } - for (c = 0; c < MAX_TABLELEN; c++) + for (int c = 0; c < MAX_TABLELEN; c++) { if (tableused[PTBL][c+1]) { @@ -2982,7 +3037,7 @@ void relocator_stereo(void) } } } - for (c = 0; c < MAX_TABLELEN; c++) + for (int c = 0; c < MAX_TABLELEN; c++) { if (tableused[FTBL][c+1]) { @@ -2990,7 +3045,7 @@ void relocator_stereo(void) if (ltable[FTBL][c] < 0x80) nofiltermod = 0; } } - for (c = 0; c < MAX_TABLELEN; c++) + for (int c = 0; c < MAX_TABLELEN; c++) { if (tableused[STBL][c+1]) speedtblsize += 2; } @@ -3234,13 +3289,13 @@ void relocator_stereo(void) // Insert songtable insertlabel("mt_songtbllo"); - for (c = 0; c < songs*6; c++) + for (int c = 0; c < songs*6; c++) { sprintf(textbuffer, "mt_song%d", c); insertaddrlo(textbuffer); } insertlabel("mt_songtblhi"); - for (c = 0; c < songs*6; c++) + for (int c = 0; c < songs*6; c++) { sprintf(textbuffer, "mt_song%d", c); insertaddrhi(textbuffer); @@ -3248,13 +3303,13 @@ void relocator_stereo(void) // Insert patterntable insertlabel("mt_patttbllo"); - for (c = 0; c < patterns; c++) + for (int c = 0; c < patterns; c++) { sprintf(textbuffer, "mt_patt%d", c); insertaddrlo(textbuffer); } insertlabel("mt_patttblhi"); - for (c = 0; c < patterns; c++) + for (int c = 0; c < patterns; c++) { sprintf(textbuffer, "mt_patt%d", c); insertaddrhi(textbuffer); @@ -3293,7 +3348,7 @@ void relocator_stereo(void) } // Insert tables - for (c = 0; c < MAX_TABLES; c++) + for (int c = 0; c < MAX_TABLES; c++) { if ((c == PTBL) && (nopulse)) goto SKIPTABLE_S; if ((c == FTBL) && (nofilter)) goto SKIPTABLE_S; @@ -3349,7 +3404,7 @@ void relocator_stereo(void) // Table label insertlabel(tablerightname[c]); - for (d = 0; d < MAX_TABLELEN; d++) + for (int d = 0; d < MAX_TABLELEN; d++) { if (tableused[c][d+1]) { @@ -3426,9 +3481,9 @@ void relocator_stereo(void) } // Insert orderlists - for (c = 0; c < songs; c++) + for (int c = 0; c < songs; c++) { - for (d = 0; d < MAX_CHN; d++) + for (int d = 0; d < MAX_CHN; d++) { sprintf(textbuffer, "mt_song%d", c*6+d); insertlabel(textbuffer); @@ -3437,7 +3492,7 @@ void relocator_stereo(void) } // Insert patterns - for (c = 0; c < patterns; c++) + for (int c = 0; c < patterns; c++) { sprintf(textbuffer, "mt_patt%d", c); insertlabel(textbuffer); @@ -3445,22 +3500,22 @@ void relocator_stereo(void) } //{ - // FILE *handle = fopen("debug.s", "wb"); - // fwrite(membuf_get(&src), membuf_memlen(&src), 1, handle); - // fclose(handle); + // FILE *handle = std::fopen("debug.s", "wb"); + // std::fwrite(membuf_get(&src), membuf_memlen(&src), 1, handle); + // std::fclose(handle); //} // Assemble; on error fail in a rude way (the parser does so too) if (assemble(&src, &dest)) exit(1); - packeddata = membuf_get(&dest); + packeddata = (unsigned char*)membuf_get(&dest); packedsize = membuf_memlen(&dest); playersize = packedsize - songtblsize - songdatasize - patttblsize - pattdatasize - instrsize - wavetblsize - pulsetblsize - filttblsize - speedtblsize; // Copy author info if (playerversion & PLAYER_AUTHORINFO) { - for (c = 0; c < 32; c++) + for (int c = 0; c < 32; c++) { packeddata[32+c] = authorname[c]; // Convert 0 to space @@ -3511,17 +3566,17 @@ void relocator_stereo(void) { case FORMAT_SID: printtext(1, 14, colors.CEDIT, "SID - SIDPlay music file format "); - strcpy(packedfilter, "*.sid"); + std::strcpy(packedfilter, "*.sid"); break; case FORMAT_PRG: printtext(1, 14, colors.CEDIT, "PRG - C64 native format "); - strcpy(packedfilter, "*.prg"); + std::strcpy(packedfilter, "*.prg"); break; case FORMAT_BIN: printtext(1, 14, colors.CEDIT, "BIN - Raw binary format (no startaddress)"); - strcpy(packedfilter, "*.bin"); + std::strcpy(packedfilter, "*.bin"); break; } @@ -3560,8 +3615,8 @@ void relocator_stereo(void) if (selectdone == -1) goto PRCLEANUP_S; // By default, copy loaded song name up to the extension - memset(packedsongname, 0, sizeof packedsongname); - for (c = 0; c < strlen(loadedsongfilename); c++) + std::memset(packedsongname, 0, sizeof packedsongname); + for (size_t c = 0; c < std::strlen(loadedsongfilename); c++) { if (loadedsongfilename[c] == '.') break; packedsongname[c] = loadedsongfilename[c]; @@ -3569,15 +3624,15 @@ void relocator_stereo(void) switch (fileformat) { case FORMAT_PRG: - strcat(packedsongname, ".prg"); + std::strcat(packedsongname, ".prg"); break; case FORMAT_BIN: - strcat(packedsongname, ".bin"); + std::strcat(packedsongname, ".bin"); break; case FORMAT_SID: - strcat(packedsongname, ".sid"); + std::strcat(packedsongname, ".sid"); break; } @@ -3587,10 +3642,10 @@ void relocator_stereo(void) if (!fileselector(packedsongname, packedpath, packedfilter, "Save Music+Playroutine", 3)) goto PRCLEANUP_S; - if (strlen(packedsongname) < MAX_FILENAME-4) + if (std::strlen(packedsongname) < MAX_FILENAME-4) { int extfound = 0; - for (c = strlen(packedsongname)-1; c >= 0; c--) + for (int c = std::strlen(packedsongname)-1; c >= 0; c--) { if (packedsongname[c] == '.') extfound = 1; } @@ -3599,20 +3654,20 @@ void relocator_stereo(void) switch (fileformat) { case FORMAT_PRG: - strcat(packedsongname, ".prg"); + std::strcat(packedsongname, ".prg"); break; case FORMAT_BIN: - strcat(packedsongname, ".bin"); + std::strcat(packedsongname, ".bin"); break; case FORMAT_SID: - strcat(packedsongname, ".sid"); + std::strcat(packedsongname, ".sid"); break; } } } - songhandle = fopen(packedsongname, "wb"); + songhandle = std::fopen(packedsongname, "wb"); } if (fileformat == FORMAT_PRG) @@ -3624,7 +3679,7 @@ void relocator_stereo(void) unsigned char ident[] = {'P', 'S', 'I', 'D', 0x00, 0x03, 0x00, 0x7c}; unsigned char byte; // Identification - fwrite(ident, sizeof ident, 1, songhandle); + std::fwrite(ident, sizeof ident, 1, songhandle); // Load address byte = 0x00; @@ -3688,9 +3743,9 @@ void relocator_stereo(void) fwrite8(songhandle, byte); // Songname etc. - fwrite(songname, sizeof songname, 1, songhandle); - fwrite(authorname, sizeof authorname, 1, songhandle); - fwrite(copyrightname, sizeof copyrightname, 1, songhandle); + std::fwrite(songname, sizeof songname, 1, songhandle); + std::fwrite(authorname, sizeof authorname, 1, songhandle); + std::fwrite(copyrightname, sizeof copyrightname, 1, songhandle); // Flags byte = 0x00; @@ -3724,19 +3779,19 @@ void relocator_stereo(void) byte = (playeradr) >> 8; fwrite8(songhandle, byte); } - if ((multiplier > 1) || (!multiplier)) fwrite(speedcode, 10, 1, songhandle); + if ((multiplier > 1) || (!multiplier)) std::fwrite(speedcode, 10, 1, songhandle); } - fwrite(packeddata, packedsize, 1, songhandle); - fclose(songhandle); + std::fwrite(packeddata, packedsize, 1, songhandle); + std::fclose(songhandle); PRCLEANUP_S: membuf_free(&src); membuf_free(&dest); - if (pattwork) free(pattwork); - if (songwork) free(songwork); - if (instrwork) free(instrwork); + if (pattwork) std::free(pattwork); + if (songwork) std::free(songwork); + if (instrwork) std::free(instrwork); printmainscreen(); key = 0; rawkey = 0; diff --git a/src/reloc.h b/src/reloc.h new file mode 100644 index 0000000..4c90cab --- /dev/null +++ b/src/reloc.h @@ -0,0 +1,46 @@ +/* + * LoadTracker + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef RELOC_H +#define RELOC_H + +#define FORMAT_SID 0 +#define FORMAT_PRG 1 +#define FORMAT_BIN 2 + +#define MAX_OPTIONS 7 + +#define TYPE_NONE 0 +#define TYPE_OVERFLOW 1 +#define TYPE_JUMP 2 + +#ifndef RELOC_C +extern unsigned char pattused[MAX_PATT]; +extern unsigned char instrused[MAX_INSTR]; +extern unsigned char tableused[MAX_TABLES][MAX_TABLELEN+1]; +extern unsigned char pattmap[MAX_PATT]; +extern unsigned char instrmap[MAX_INSTR]; +extern unsigned char tablemap[MAX_TABLES][MAX_TABLELEN+1]; +extern int tableerror; +#endif + +void relocator(); +void relocator_stereo(); +unsigned char swapnybbles(unsigned char n); + +#endif diff --git a/src/gsid.cpp b/src/sid.cpp similarity index 85% rename from src/gsid.cpp rename to src/sid.cpp index 02aa6a1..31582c5 100644 --- a/src/gsid.cpp +++ b/src/sid.cpp @@ -1,17 +1,34 @@ /* - * ============================================================================= - * reSIDfp interface - * ============================================================================= + * LoadTracker + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#define GSID_C +// ============================================================================= +// reSIDfp interface +// ============================================================================= + +#define SID_C + +#include "sid.h" + +#include "sound.h" -#include -#include #include -#include "gsid.h" -#include "gsound.h" +#include int clockrate; int samplerate; @@ -128,7 +145,7 @@ int sid_fillbuffer(short *ptr, int samples) int result = 0; int total = 0; - int badline = rand() % NUMSIDREGS; + int badline = std::rand() % NUMSIDREGS; int tdelta = clockrate * samples / samplerate; if (tdelta <= 0) return total; diff --git a/src/sid.h b/src/sid.h new file mode 100644 index 0000000..bcb5af8 --- /dev/null +++ b/src/sid.h @@ -0,0 +1,39 @@ +/* + * LoadTracker + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef SID_H +#define SID_H + +#define NUMSIDREGS 0x19 +#define SIDWRITEDELAY 14 // lda $xxxx,x 4 cycles, sta $d400,x 5 cycles, dex 2 cycles, bpl 3 cycles +#define SIDWAVEDELAY 4 // and $xxxx,x 4 cycles extra + +void sid_init(int speed, unsigned m, + unsigned ntsc, unsigned interpolate, + unsigned customclockrate, unsigned numsids, + float filterbias, unsigned combwaves); +int sid_fillbuffer(short *ptr, int samples); +int sid_fillbuffer_stereo(short *lptr, short *rptr, int samples); +unsigned char sid_getorder(unsigned char index); + +#ifndef SID_C +extern unsigned char sidreg[NUMSIDREGS]; +extern unsigned char sidreg2[NUMSIDREGS]; +#endif + +#endif diff --git a/src/gsong.c b/src/song.cpp similarity index 80% rename from src/gsong.c rename to src/song.cpp index fe325d6..cdd0f8b 100644 --- a/src/gsong.c +++ b/src/song.cpp @@ -1,12 +1,32 @@ /* - * ============================================================================= - * song data model, loading/saving/conversion - * ============================================================================= + * LoadTracker + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#define GSONG_C +// ============================================================================= +// song data model, loading/saving/conversion +// ============================================================================= + +#define SONG_C #include "loadtrk.h" +#include "bme_end.h" + +#include +#include INSTR instr[MAX_INSTR]; unsigned char ltable[MAX_TABLES][MAX_TABLELEN]; @@ -23,38 +43,33 @@ int songlen_stereo[MAX_SONGS][MAX_CHN]; int highestusedpattern; int highestusedinstr; +void findduplicatepatterns(); int determinechannels(FILE* handle); -int savesong(void) +int savesong() { - int c; char ident[] = {'G', 'T', 'S', '5'}; - FILE *handle; int maxChns = MAX_CHN; - if (numsids == 1) maxChns = 3; + if (numsids == 1) maxChns = MAX_CHN_MONO; - if (strlen(songfilename) < MAX_FILENAME-4) + if (std::strlen(songfilename) < MAX_FILENAME-4) { int extfound = 0; - for (c = strlen(songfilename)-1; c >= 0; c--) + for (int c = std::strlen(songfilename)-1; c >= 0; c--) { if (songfilename[c] == '.') extfound = 1; } - if (!extfound) strcat(songfilename, ".sng"); + if (!extfound) std::strcat(songfilename, ".sng"); } - handle = fopen(songfilename, "wb"); + FILE *handle = std::fopen(songfilename, "wb"); if (handle) { - int d; - int length; - int amount; - int writebytes; - fwrite(ident, 4, 1, handle); + std::fwrite(ident, 4, 1, handle); // Determine amount of patterns & instruments countpatternlengths(); - for (c = 1; c < MAX_INSTR; c++) + for (int c = 1; c < MAX_INSTR; c++) { if ((instr[c].ad) || (instr[c].sr) || (instr[c].ptr[0]) || (instr[c].ptr[1]) || (instr[c].ptr[2]) || (instr[c].vibdelay) || (instr[c].ptr[3])) @@ -64,12 +79,12 @@ int savesong(void) } // Write infotexts - fwrite(songname, sizeof songname, 1, handle); - fwrite(authorname, sizeof authorname, 1, handle); - fwrite(copyrightname, sizeof copyrightname, 1, handle); + std::fwrite(songname, sizeof songname, 1, handle); + std::fwrite(authorname, sizeof authorname, 1, handle); + std::fwrite(copyrightname, sizeof copyrightname, 1, handle); // Determine amount of songs to be saved - c = MAX_SONGS - 1; + int c = MAX_SONGS - 1; if (numsids == 1) { for (;;) @@ -92,36 +107,36 @@ int savesong(void) c--; } } - amount = c + 1; + int amount = c + 1; fwrite8(handle, amount); // Write songorderlists - for (d = 0; d < amount; d++) + for (int d = 0; d < amount; d++) { - for (c = 0; c < maxChns; c++) + for (int c = 0; c < maxChns; c++) { if (numsids == 1) { - length = songlen[d][c]+1; + int length = songlen[d][c]+1; fwrite8(handle, length); - writebytes = length; + int writebytes = length; writebytes++; - fwrite(songorder[d][c], writebytes, 1, handle); + std::fwrite(songorder[d][c], writebytes, 1, handle); } else if (numsids == 2) { - length = songlen_stereo[d][c]+1; + int length = songlen_stereo[d][c]+1; fwrite8(handle, length); - writebytes = length; + int writebytes = length; writebytes++; - fwrite(songorder_stereo[d][c], writebytes, 1, handle); + std::fwrite(songorder_stereo[d][c], writebytes, 1, handle); } } } // Write amount of instruments fwrite8(handle, highestusedinstr); // Write instruments - for (c = 1; c <= highestusedinstr; c++) + for (int c = 1; c <= highestusedinstr; c++) { fwrite8(handle, instr[c].ad); fwrite8(handle, instr[c].sr); @@ -135,49 +150,47 @@ int savesong(void) fwrite(&instr[c].name, MAX_INSTRNAMELEN, 1, handle); } // Write tables - for (c = 0; c < MAX_TABLES; c++) + for (int c = 0; c < MAX_TABLES; c++) { - writebytes = gettablelen(c); + int writebytes = gettablelen(c); fwrite8(handle, writebytes); - fwrite(ltable[c], writebytes, 1, handle); - fwrite(rtable[c], writebytes, 1, handle); + std::fwrite(ltable[c], writebytes, 1, handle); + std::fwrite(rtable[c], writebytes, 1, handle); } // Write patterns amount = highestusedpattern + 1; fwrite8(handle, amount); for (c = 0; c < amount; c++) { - length = pattlen[c]+1; + int length = pattlen[c]+1; fwrite8(handle, length); fwrite(pattern[c], length * 4, 1, handle); } - fclose(handle); - strcpy(loadedsongfilename, songfilename); + std::fclose(handle); + std::strcpy(loadedsongfilename, songfilename); return 1; } return 0; } -int saveinstrument(void) +int saveinstrument() { - int c; char ident[] = {'G', 'T', 'I', '5'}; - FILE *handle; - if (strlen(instrfilename) < MAX_FILENAME-4) + if (std::strlen(instrfilename) < MAX_FILENAME-4) { int extfound = 0; - for (c = strlen(instrfilename)-1; c >= 0; c--) + for (int c = std::strlen(instrfilename)-1; c >= 0; c--) { if (instrfilename[c] == '.') extfound = 1; } - if (!extfound) strcat(instrfilename, ".ins"); + if (!extfound) std::strcat(instrfilename, ".ins"); } - handle = fopen(instrfilename, "wb"); + FILE *handle = std::fopen(instrfilename, "wb"); if (handle) { - fwrite(ident, 4, 1, handle); + std::fwrite(ident, 4, 1, handle); // Write instrument fwrite8(handle, instr[einum].ad); @@ -190,42 +203,40 @@ int saveinstrument(void) fwrite8(handle, instr[einum].gatetimer); fwrite8(handle, instr[einum].firstwave); fwrite(&instr[einum].name, MAX_INSTRNAMELEN, 1, handle); - for (c = 0; c < MAX_TABLES; c++) + for (int c = 0; c < MAX_TABLES; c++) { if (instr[einum].ptr[c]) { int pos = instr[einum].ptr[c] - 1; int len = gettablepartlen(c, pos); fwrite8(handle, len); - fwrite(<able[c][pos], len, 1, handle); - fwrite(&rtable[c][pos], len, 1, handle); + std::fwrite(<able[c][pos], len, 1, handle); + std::fwrite(&rtable[c][pos], len, 1, handle); } else fwrite8(handle, 0); } - fclose(handle); + std::fclose(handle); return 1; } return 0; } -void loadsong(void) +void loadsong() { - int c; - int ok = 0; - char ident[4]; - FILE *handle; int maxChns = MAX_CHN; - if (numsids == 1) maxChns = 3; + if (numsids == 1) maxChns = MAX_CHN_MONO; int channelstoload = maxChns; - handle = fopen(songfilename, "rb"); + FILE *handle = std::fopen(songfilename, "rb"); + + int ok = 0; + char ident[4]; if (handle) { fread(ident, 4, 1, handle); - if ((!memcmp(ident, "GTS3", 4)) || (!memcmp(ident, "GTS4", 4)) || (!memcmp(ident, "GTS5", 4))) + if ((!std::memcmp(ident, "GTS3", 4)) || (!std::memcmp(ident, "GTS4", 4)) || (!std::memcmp(ident, "GTS5", 4))) { - int d; int length; int amount; int loadsize; @@ -233,33 +244,33 @@ void loadsong(void) ok = 1; // Read infotexts - fread(songname, sizeof songname, 1, handle); - fread(authorname, sizeof authorname, 1, handle); - fread(copyrightname, sizeof copyrightname, 1, handle); + std::fread(songname, sizeof songname, 1, handle); + std::fread(authorname, sizeof authorname, 1, handle); + std::fread(copyrightname, sizeof copyrightname, 1, handle); // Read songorderlists channelstoload = determinechannels(handle); amount = fread8(handle); - for (d = 0; d < amount; d++) + for (int d = 0; d < amount; d++) { - for (c = 0; c < channelstoload; c++) + for (int c = 0; c < channelstoload; c++) { length = fread8(handle); loadsize = length; loadsize++; if (numsids == 1) { - fread(songorder[d][c], loadsize, 1, handle); + std::fread(songorder[d][c], loadsize, 1, handle); } else if (numsids == 2) { - fread(songorder_stereo[d][c], loadsize, 1, handle); + std::fread(songorder_stereo[d][c], loadsize, 1, handle); } } } // Read instruments amount = fread8(handle); - for (c = 1; c <= amount; c++) + for (int c = 1; c <= amount; c++) { instr[c].ad = fread8(handle); instr[c].sr = fread8(handle); @@ -270,30 +281,29 @@ void loadsong(void) instr[c].vibdelay = fread8(handle); instr[c].gatetimer = fread8(handle); instr[c].firstwave = fread8(handle); - fread(&instr[c].name, MAX_INSTRNAMELEN, 1, handle); + std::fread(&instr[c].name, MAX_INSTRNAMELEN, 1, handle); } // Read tables - for (c = 0; c < MAX_TABLES; c++) + for (int c = 0; c < MAX_TABLES; c++) { loadsize = fread8(handle); - fread(ltable[c], loadsize, 1, handle); - fread(rtable[c], loadsize, 1, handle); + std::fread(ltable[c], loadsize, 1, handle); + std::fread(rtable[c], loadsize, 1, handle); } // Read patterns amount = fread8(handle); - for (c = 0; c < amount; c++) + for (int c = 0; c < amount; c++) { length = fread8(handle) * 4; - fread(pattern[c], length, 1, handle); + std::fread(pattern[c], length, 1, handle); } countpatternlengths(); songchange(); } // Goattracker v2.xx (3-table) import - if (!memcmp(ident, "GTS2", 4)) + if (!std::memcmp(ident, "GTS2", 4)) { - int d; int length; int amount; int loadsize; @@ -308,26 +318,26 @@ void loadsong(void) // Read songorderlists channelstoload = determinechannels(handle); amount = fread8(handle); - for (d = 0; d < amount; d++) + for (int d = 0; d < amount; d++) { - for (c = 0; c < channelstoload; c++) + for (int c = 0; c < channelstoload; c++) { length = fread8(handle); loadsize = length; loadsize++; if (numsids == 1) { - fread(songorder[d][c], loadsize, 1, handle); + std::fread(songorder[d][c], loadsize, 1, handle); } else if (numsids == 2) { - fread(songorder_stereo[d][c], loadsize, 1, handle); + std::fread(songorder_stereo[d][c], loadsize, 1, handle); } } } // Read instruments amount = fread8(handle); - for (c = 1; c <= amount; c++) + for (int c = 1; c <= amount; c++) { instr[c].ad = fread8(handle); instr[c].sr = fread8(handle); @@ -338,25 +348,24 @@ void loadsong(void) instr[c].ptr[STBL] = makespeedtable(fread8(handle), finevibrato, 0) + 1; instr[c].gatetimer = fread8(handle); instr[c].firstwave = fread8(handle); - fread(&instr[c].name, MAX_INSTRNAMELEN, 1, handle); + std::fread(&instr[c].name, MAX_INSTRNAMELEN, 1, handle); } // Read tables - for (c = 0; c < MAX_TABLES-1; c++) + for (int c = 0; c < MAX_TABLES-1; c++) { loadsize = fread8(handle); - fread(ltable[c], loadsize, 1, handle); - fread(rtable[c], loadsize, 1, handle); + std::fread(ltable[c], loadsize, 1, handle); + std::fread(rtable[c], loadsize, 1, handle); } // Read patterns amount = fread8(handle); - for (c = 0; c < amount; c++) + for (int c = 0; c < amount; c++) { - int d; length = fread8(handle) * 4; - fread(pattern[c], length, 1, handle); + std::fread(pattern[c], length, 1, handle); // Convert speedtable-requiring commands - for (d = 0; d < length; d++) + for (int d = 0; d < length; d++) { switch (pattern[c][d*4+2]) { @@ -380,9 +389,8 @@ void loadsong(void) songchange(); } // Goattracker 1.xx import - if (!memcmp(ident, "GTS!", 4)) + if (!std::memcmp(ident, "GTS!", 4)) { - int d; int length; int amount; int loadsize; @@ -401,33 +409,33 @@ void loadsong(void) ok = 1; // Read infotexts - fread(songname, sizeof songname, 1, handle); - fread(authorname, sizeof authorname, 1, handle); - fread(copyrightname, sizeof copyrightname, 1, handle); + std::fread(songname, sizeof songname, 1, handle); + std::fread(authorname, sizeof authorname, 1, handle); + std::fread(copyrightname, sizeof copyrightname, 1, handle); // Read songorderlists channelstoload = determinechannels(handle); amount = fread8(handle); - for (d = 0; d < amount; d++) + for (int d = 0; d < amount; d++) { - for (c = 0; c < channelstoload; c++) + for (int c = 0; c < channelstoload; c++) { length = fread8(handle); loadsize = length; loadsize++; if (numsids == 1) { - fread(songorder[d][c], loadsize, 1, handle); + std::fread(songorder[d][c], loadsize, 1, handle); } else if (numsids == 2) { - fread(songorder_stereo[d][c], loadsize, 1, handle); + std::fread(songorder_stereo[d][c], loadsize, 1, handle); } } } // Convert instruments - for (c = 1; c < 32; c++) + for (int c = 1; c < 32; c++) { unsigned char wavelen; @@ -442,11 +450,11 @@ void loadsong(void) if (pulse[c] & 1) instr[c].gatetimer |= 0x80; // "No hardrestart" flag pulse[c] &= 0xfe; wavelen = fread8(handle)/2; - fread(&instr[c].name, MAX_INSTRNAMELEN, 1, handle); + std::fread(&instr[c].name, MAX_INSTRNAMELEN, 1, handle); instr[c].ptr[WTBL] = fw+1; // Convert wavetable - for (d = 0; d < wavelen; d++) + for (int d = 0; d < wavelen; d++) { if (fw < MAX_TABLELEN) { @@ -482,7 +490,7 @@ void loadsong(void) int pulsetime, pulsedist, hlpos; // Check for duplicate pulse settings - for (d = 1; d < c; d++) + for (int d = 1; d < c; d++) { if ((pulse[d] == pulse[c]) && (pulseadd[d] == pulseadd[c]) && (pulselimitlow[d] == pulselimitlow[c]) && (pulselimithigh[d] == pulselimithigh[c])) @@ -602,10 +610,10 @@ void loadsong(void) } // Convert patterns amount = fread8(handle); - for (c = 0; c < amount; c++) + for (int c = 0; c < amount; c++) { length = fread8(handle); - for (d = 0; d < length/3; d++) + for (int d = 0; d < length/3; d++) { unsigned char note, cmd, data, instr; note = fread8(handle); @@ -660,10 +668,10 @@ void loadsong(void) songchange(); // Read filtertable - fread(filtertable, 256, 1, handle); + std::fread(filtertable, 256, 1, handle); // Convert filtertable - for (c = 0; c < 64; c++) + for (int c = 0; c < 64; c++) { filterjumppos[c] = -1; filtermap[c] = 0; @@ -672,7 +680,7 @@ void loadsong(void) if (numfilter > 63) numfilter = 63; - for (c = 1; c <= numfilter; c++) + for (int c = 1; c <= numfilter; c++) { filtermap[c] = ff+1; @@ -719,22 +727,22 @@ void loadsong(void) } // Now fix jumps as the filterstep mapping is known - for (c = 1; c <= numfilter; c++) + for (int c = 1; c <= numfilter; c++) { if (filterjumppos[c] != -1) rtable[FTBL][filterjumppos[c]] = filtermap[rtable[FTBL][filterjumppos[c]]]; } // Fix filterpointers in instruments - for (c = 1; c < 32; c++) + for (int c = 1; c < 32; c++) instr[c].ptr[FTBL] = filtermap[instr[c].ptr[FTBL]]; // Now fix pattern commands - memset(arpmap, 0, sizeof arpmap); - for (c = 0; c < MAX_PATT; c++) + std::memset(arpmap, 0, sizeof arpmap); + for (int c = 0; c < MAX_PATT; c++) { unsigned char i = 0; - for (d = 0; d <= MAX_PATTROWS; d++) + for (int d = 0; d <= MAX_PATTROWS; d++) { if (pattern[c][d*4+1]) i = pattern[c][d*4+1]; @@ -865,19 +873,19 @@ void loadsong(void) } } } - fclose(handle); + std::fclose(handle); } if (ok) { - strcpy(loadedsongfilename, songfilename); + std::strcpy(loadedsongfilename, songfilename); // Reset table views - for (c = 0; c < MAX_TABLES; c++) settableview(c, 0); + for (int c = 0; c < MAX_TABLES; c++) settableview(c, 0); // Convert pulsemodulation speed of < v2.4 songs if (ident[3] < '4') { - for (c = 0; c < MAX_TABLELEN; c++) + for (int c = 0; c < MAX_TABLELEN; c++) { if ((ltable[PTBL][c] < 0x80) && (rtable[PTBL][c])) { @@ -893,7 +901,7 @@ void loadsong(void) // Convert old legato/nohr parameters if (ident[3] < '5') { - for (c = 1; c < MAX_INSTR; c++) + for (int c = 1; c < MAX_INSTR; c++) { if (instr[c].firstwave >= 0x80) { @@ -910,7 +918,7 @@ void loadsong(void) int emptypatt = MAX_PATT-1; findusedpatterns(); - for (c = 0; c < MAX_PATT; c++) + for (int c = 0; c < MAX_PATT; c++) { if (!pattused[c]) { @@ -931,12 +939,11 @@ void loadsong(void) } } - for (c = 0; c < MAX_SONGS; c++) + for (int c = 0; c < MAX_SONGS; c++) { if (songlen_stereo[c][0]) { - int d; - for (d = channelstoload; d < MAX_CHN; d++) + for (int d = channelstoload; d < MAX_CHN; d++) { songorder_stereo[c][d][0] = emptypatt; songorder_stereo[c][d][1] = 0xff; @@ -950,21 +957,19 @@ void loadsong(void) } } -void loadinstrument(void) +void loadinstrument() { char ident[4]; - FILE *handle; - int c,d; int pulsestart = -1; int pulseend = -1; - handle = fopen(instrfilename, "rb"); + FILE *handle = std::fopen(instrfilename, "rb"); if (handle) { stopsong(); - fread(ident, 4, 1, handle); + std::fread(ident, 4, 1, handle); - if ((!memcmp(ident, "GTI3", 4)) || (!memcmp(ident, "GTI4", 4)) || (!memcmp(ident, "GTI5", 4))) + if ((!std::memcmp(ident, "GTI3", 4)) || (!std::memcmp(ident, "GTI4", 4)) || (!std::memcmp(ident, "GTI5", 4))) { unsigned char optr[4]; @@ -977,19 +982,20 @@ void loadinstrument(void) instr[einum].vibdelay = fread8(handle); instr[einum].gatetimer = fread8(handle); instr[einum].firstwave = fread8(handle); - fread(&instr[einum].name, MAX_INSTRNAMELEN, 1, handle); + std::fread(&instr[einum].name, MAX_INSTRNAMELEN, 1, handle); // Erase old tabledata deleteinstrtable(einum); // Load new tabledata - for (c = 0; c < MAX_TABLES; c++) + for (int c = 0; c < MAX_TABLES; c++) { int start = gettablelen(c); int len = fread8(handle); if (len) { + int d; for (d = start; (d < start+len) && (d < MAX_TABLELEN); d++) ltable[c][d] = fread8(handle); while (d < start+len) @@ -1006,7 +1012,7 @@ void loadinstrument(void) } if (c != STBL) { - for (d = start; (d < start+len) && (d < MAX_TABLELEN); d++) + for (int d = start; (d < start+len) && (d < MAX_TABLELEN); d++) { if (ltable[c][d] == 0xff) { @@ -1027,7 +1033,7 @@ void loadinstrument(void) } // Goattracker v2.xx (3-table) import - if (!memcmp(ident, "GTI2", 4)) + if (!std::memcmp(ident, "GTI2", 4)) { unsigned char optr[3]; @@ -1040,19 +1046,20 @@ void loadinstrument(void) instr[einum].ptr[STBL] = makespeedtable(fread8(handle), finevibrato, 0) + 1; instr[einum].gatetimer = fread8(handle); instr[einum].firstwave = fread8(handle); - fread(&instr[einum].name, MAX_INSTRNAMELEN, 1, handle); + std::fread(&instr[einum].name, MAX_INSTRNAMELEN, 1, handle); // Erase old tabledata deleteinstrtable(einum); // Load new tabledata - for (c = 0; c < MAX_TABLES-1; c++) + for (int c = 0; c < MAX_TABLES-1; c++) { int start = gettablelen(c); int len = fread8(handle); if (len) { + int d; for (d = start; (d < start+len) && (d < MAX_TABLELEN); d++) ltable[c][d] = fread8(handle); while (d < start+len) @@ -1067,7 +1074,7 @@ void loadinstrument(void) fread8(handle); d++; } - for (d = start; (d < start+len) && (d < MAX_TABLELEN); d++) + for (int d = start; (d < start+len) && (d < MAX_TABLELEN); d++) { if (ltable[c][d] == 0xff) { @@ -1086,7 +1093,7 @@ void loadinstrument(void) } } // Goattracker 1.xx import - if (!memcmp(ident, "GTI!", 4)) + if (!std::memcmp(ident, "GTI!", 4)) { unsigned char pulse, pulseadd, pulselimitlow, pulselimithigh, wavelen; @@ -1118,7 +1125,7 @@ void loadinstrument(void) instr[einum].ptr[WTBL] = fw+1; // Convert wavetable - for (d = 0; d < wavelen; d++) + for (int d = 0; d < wavelen; d++) { if (fw < MAX_TABLELEN) { @@ -1301,13 +1308,13 @@ void loadinstrument(void) ff++; } } - - fclose(handle); + + std::fclose(handle); // Convert pulsemodulation speed of < v2.4 instruments if ((ident[3] < '4') && (pulsestart != -1)) { - for (c = pulsestart; (c < pulseend) && (c < MAX_TABLELEN); c++) + for (int c = pulsestart; (c < pulseend) && (c < MAX_TABLELEN); c++) { if ((ltable[PTBL][c] < 0x80) && (rtable[PTBL][c])) { @@ -1334,9 +1341,8 @@ void loadinstrument(void) void clearsong(int cs, int cp, int ci, int ct, int cn) { - int c; int maxChns = MAX_CHN; - if (numsids == 1) maxChns = 3; + if (numsids == 1) maxChns = MAX_CHN_MONO; if (!(cs | cp | ci | ct | cn)) return; @@ -1348,9 +1354,8 @@ void clearsong(int cs, int cp, int ci, int ct, int cn) esmarkchn = -1; followplay = 0; - for (c = 0; c < maxChns; c++) + for (int c = 0; c < maxChns; c++) { - int d; chn[c].mute = 0; if (multiplier) chn[c].tempo = multiplier*6-1; @@ -1359,12 +1364,12 @@ void clearsong(int cs, int cp, int ci, int ct, int cn) chn[c].pattptr = 0; if (cs) { - memset(loadedsongfilename, 0, sizeof loadedsongfilename); - for (d = 0; d < MAX_SONGS; d++) + std::memset(loadedsongfilename, 0, sizeof loadedsongfilename); + for (int d = 0; d < MAX_SONGS; d++) { if (numsids == 1) { - memset(&songorder[d][c][0], 0, MAX_SONGLEN+2); + std::memset(&songorder[d][c][0], 0, MAX_SONGLEN+2); if (!d) { songorder[d][c][0] = c; @@ -1377,7 +1382,7 @@ void clearsong(int cs, int cp, int ci, int ct, int cn) } else if (numsids == 2) { - memset(&songorder_stereo[d][c][0], 0, MAX_SONGLEN+2); + std::memset(&songorder_stereo[d][c][0], 0, MAX_SONGLEN+2); if (!d) { songorder_stereo[d][c][0] = c; @@ -1415,48 +1420,48 @@ void clearsong(int cs, int cp, int ci, int ct, int cn) } if (cn) { - memset(songname, 0, sizeof songname); - memset(authorname, 0, sizeof authorname); - memset(copyrightname, 0, sizeof copyrightname); + std::memset(songname, 0, sizeof songname); + std::memset(authorname, 0, sizeof authorname); + std::memset(copyrightname, 0, sizeof copyrightname); enpos = 0; } if (cp) { - memset(loadedsongfilename, 0, sizeof loadedsongfilename); - for (c = 0; c < MAX_PATT; c++) + std::memset(loadedsongfilename, 0, sizeof loadedsongfilename); + for (int c = 0; c < MAX_PATT; c++) clearpattern(c); } if (ci) { - for (c = 0; c < MAX_INSTR; c++) + for (int c = 0; c < MAX_INSTR; c++) clearinstr(c); - memset(&instrcopybuffer, 0, sizeof(INSTR)); + std::memset(&instrcopybuffer, 0, sizeof(INSTR)); eipos = 0; eicolumn = 0; einum = 1; } if (ct == 1) { - for (c = MAX_TABLES-1; c >= 0; c--) + for (int c = MAX_TABLES-1; c >= 0; c--) { - memset(ltable[c], 0, MAX_TABLELEN); - memset(rtable[c], 0, MAX_TABLELEN); + std::memset(ltable[c], 0, MAX_TABLELEN); + std::memset(rtable[c], 0, MAX_TABLELEN); settableview(c, 0); } } countpatternlengths(); } -void countpatternlengths(void) +void countpatternlengths() { - int c, d, e; int maxChns = MAX_CHN; if (numsids == 1) maxChns = 3; highestusedpattern = 0; highestusedinstr = 0; - for (c = 0; c < MAX_PATT; c++) + for (int c = 0; c < MAX_PATT; c++) { + int d; for (d = 0; d <= MAX_PATTROWS; d++) { if (pattern[c][d*4] == ENDPATT) break; @@ -1467,10 +1472,11 @@ void countpatternlengths(void) pattlen[c] = d; } - for (e = 0; e < MAX_SONGS; e++) + for (int e = 0; e < MAX_SONGS; e++) { - for (c = 0; c < maxChns; c++) + for (int c = 0; c < maxChns; c++) { + int d; for (d = 0; d < MAX_SONGLEN; d++) { if (numsids == 1) @@ -1494,30 +1500,29 @@ void countpatternlengths(void) } if (numsids == 1) { - songlen[e][c] = d; + songlen[e][c] = d; } else if (numsids == 2) { - songlen_stereo[e][c] = d; + songlen_stereo[e][c] = d; } } } } -void countthispattern(void) +void countthispattern() { - int c, d, e; - - c = epnum[epchn]; + int c = epnum[epchn]; + int d; for (d = 0; d <= MAX_PATTROWS; d++) { if (pattern[c][d*4] == ENDPATT) break; } pattlen[c] = d; - e = esnum; + int e = esnum; c = eschn; - for (d = 0; d < MAX_SONGLEN; d++) + for (int d = 0; d < MAX_SONGLEN; d++) { if (numsids == 1) { @@ -1548,25 +1553,24 @@ void countthispattern(void) int insertpattern(int p) { - int c, d, e; - int tmp = 0; int maxChns = MAX_CHN; - if (numsids == 1) maxChns = 3; + if (numsids == 1) maxChns = MAX_CHN_MONO; findusedpatterns(); if (p >= MAX_PATT-2) return 0; if (pattused[MAX_PATT-1]) return 0; - memmove(pattern[p+2], pattern[p+1], (MAX_PATT-p-2)*(MAX_PATTROWS*4+4)); + std::memmove(pattern[p+2], pattern[p+1], (MAX_PATT-p-2)*(MAX_PATTROWS*4+4)); countpatternlengths(); - for (c = 0; c < MAX_SONGS; c++) + for (int c = 0; c < MAX_SONGS; c++) { if (((numsids==1)&&(songlen[c][0])&&(songlen[c][1])&&(songlen[c][2]))|| ((numsids==2)&&(songlen_stereo[c][0])&& (songlen_stereo[c][1])&&(songlen_stereo[c][2]))) { - for (d = 0; d < maxChns; d++) + for (int d = 0; d < maxChns; d++) { + int tmp = 0; if (numsids == 1) { tmp = songlen[c][d]; @@ -1575,7 +1579,7 @@ int insertpattern(int p) { tmp = songlen_stereo[c][d]; } - for (e = 0; e < tmp; e++) + for (int e = 0; e < tmp; e++) { if (numsids == 1) { @@ -1600,7 +1604,7 @@ int insertpattern(int p) } } - for (c = 0; c < maxChns; c++) + for (int c = 0; c < maxChns; c++) { if ((epnum[c] > p) && (epnum[c] != MAX_PATT-1)) epnum[c]++; } @@ -1610,25 +1614,24 @@ int insertpattern(int p) void deletepattern(int p) { - int c, d, e; - int tmp; int maxChns = MAX_CHN; - if (numsids == 1) maxChns = 3; + if (numsids == 1) maxChns = MAX_CHN_MONO; if (p == MAX_PATT-1) return; - memmove(pattern[p], pattern[p+1], (MAX_PATT-p-1)*(MAX_PATTROWS*4+4)); + std::memmove(pattern[p], pattern[p+1], (MAX_PATT-p-1)*(MAX_PATTROWS*4+4)); clearpattern(MAX_PATT-1); countpatternlengths(); - for (c = 0; c < MAX_SONGS; c++) + for (int c = 0; c < MAX_SONGS; c++) { if (((numsids==1)&&(songlen[c][0])&&(songlen[c][1])&&(songlen[c][2]))|| ((numsids==2)&&(songlen_stereo[c][0])&& (songlen_stereo[c][1])&&(songlen_stereo[c][2]))) { - for (d = 0; d < maxChns; d++) + for (int d = 0; d < maxChns; d++) { + int tmp = 0; if (numsids == 1) { tmp = songlen[c][d]; @@ -1637,7 +1640,7 @@ void deletepattern(int p) { tmp = songlen_stereo[c][d]; } - for (e = 0; e < tmp; e++) + for (int e = 0; e < tmp; e++) { if (numsids == 1) { @@ -1659,8 +1662,8 @@ void deletepattern(int p) } } } - - for (c = 0; c < maxChns; c++) + + for (int c = 0; c < maxChns; c++) { if (epnum[c] > p) epnum[c]--; } @@ -1668,30 +1671,27 @@ void deletepattern(int p) void clearpattern(int p) { - int c; - - memset(pattern[p], 0, MAX_PATTROWS*4); - for (c = 0; c < defaultpatternlength; c++) pattern[p][c*4] = REST; - for (c = defaultpatternlength; c <= MAX_PATTROWS; c++) pattern[p][c*4] = ENDPATT; + std::memset(pattern[p], 0, MAX_PATTROWS*4); + for (int c = 0; c < defaultpatternlength; c++) pattern[p][c*4] = REST; + for (int c = defaultpatternlength; c <= MAX_PATTROWS; c++) pattern[p][c*4] = ENDPATT; } -void findusedpatterns(void) +void findusedpatterns() { - int c, d, e; - int tmp; int maxChns = MAX_CHN; - if (numsids == 1) maxChns = 3; + if (numsids == 1) maxChns = MAX_CHN_MONO; countpatternlengths(); - memset(pattused, 0, sizeof pattused); - for (c = 0; c < MAX_SONGS; c++) + std::memset(pattused, 0, sizeof pattused); + for (int c = 0; c < MAX_SONGS; c++) { if (((numsids==1)&&(songlen[c][0])&&(songlen[c][1])&&(songlen[c][2]))|| ((numsids==2)&&(songlen_stereo[c][0])&& (songlen_stereo[c][1])&&(songlen_stereo[c][2]))) { - for (d = 0; d < maxChns; d++) + for (int d = 0; d < maxChns; d++) { + int tmp = 0; if (numsids == 1) { tmp = songlen[c][d]; @@ -1700,7 +1700,7 @@ void findusedpatterns(void) { tmp = songlen_stereo[c][d]; } - for (e = 0; e < tmp; e++) + for (int e = 0; e < tmp; e++) { if (numsids == 1) { @@ -1722,37 +1722,34 @@ void findusedpatterns(void) } } -void findduplicatepatterns(void) +void findduplicatepatterns() { - int c, d; int maxChns = MAX_CHN; - if (numsids == 1) maxChns = 3; + if (numsids == 1) maxChns = MAX_CHN_MONO; findusedpatterns(); if (numsids == 1) { - for (c = 0; c < MAX_PATT; c++) + for (int c = 0; c < MAX_PATT; c++) { if (pattused[c]) { - for (d = c+1; d < MAX_PATT; d++) + for (int d = c+1; d < MAX_PATT; d++) { if (pattlen[d] == pattlen[c]) { - if (!memcmp(pattern[c], pattern[d], pattlen[c]*4)) + if (!std::memcmp(pattern[c], pattern[d], pattlen[c]*4)) { - int f, g, h; - - for (f = 0; f < MAX_SONGS; f++) + for (int f = 0; f < MAX_SONGS; f++) { if ((songlen[f][0]) && (songlen[f][1]) && (songlen[f][2])) { - for (g = 0; g < maxChns; g++) + for (int g = 0; g < maxChns; g++) { - for (h = 0; h < songlen[f][g]; h++) + for (int h = 0; h < songlen[f][g]; h++) { if (songorder[f][g][h] == d) songorder[f][g][h] = c; @@ -1760,7 +1757,7 @@ void findduplicatepatterns(void) } } } - for (f = 0; f < maxChns; f++) + for (int f = 0; f < maxChns; f++) if (epnum[f] == d) epnum[f] = c; } } @@ -1770,27 +1767,25 @@ void findduplicatepatterns(void) } else if (numsids == 2) { - for (c = 0; c < MAX_PATT; c++) + for (int c = 0; c < MAX_PATT; c++) { if (pattused[c]) { - for (d = c+1; d < MAX_PATT; d++) + for (int d = c+1; d < MAX_PATT; d++) { if (pattlen[d] == pattlen[c]) { - if (!memcmp(pattern[c], pattern[d], pattlen[c]*4)) + if (!std::memcmp(pattern[c], pattern[d], pattlen[c]*4)) { - int f, g, h; - - for (f = 0; f < MAX_SONGS; f++) + for (int f = 0; f < MAX_SONGS; f++) { if ((songlen_stereo[f][0]) && (songlen_stereo[f][1]) && (songlen_stereo[f][2])) { - for (g = 0; g < maxChns; g++) + for (int g = 0; g < maxChns; g++) { - for (h = 0; h < songlen_stereo[f][g]; h++) + for (int h = 0; h < songlen_stereo[f][g]; h++) { if (songorder_stereo[f][g][h] == d) { @@ -1800,7 +1795,7 @@ void findduplicatepatterns(void) } } } - for (f = 0; f < maxChns; f++) + for (int f = 0; f < maxChns; f++) if (epnum[f] == d) epnum[f] = c; } } @@ -1814,19 +1809,17 @@ void findduplicatepatterns(void) void optimizeeverything(int oi, int ot) { - int c, d, e; - stopsong(); findduplicatepatterns(); - memset(instrused, 0, sizeof instrused); + std::memset(instrused, 0, sizeof instrused); - for (c = MAX_PATT-1; c >= 0; c--) + for (int c = MAX_PATT-1; c >= 0; c--) { if (pattused[c]) { - for (d = 0; d < MAX_PATTROWS; d++) + for (int d = 0; d < MAX_PATTROWS; d++) { if (pattern[c][d*4] == ENDPATT) break; if (pattern[c][d*4+1]) @@ -1840,7 +1833,7 @@ void optimizeeverything(int oi, int ot) if (oi) { - for (c = MAX_INSTR-2; c >= 1; c--) + for (int c = MAX_INSTR-2; c >= 1; c--) { if (!instrused[c]) { @@ -1848,11 +1841,11 @@ void optimizeeverything(int oi, int ot) if (c < MAX_INSTR-2) { - memmove(&instr[c], &instr[c+1], (MAX_INSTR-2-c) * sizeof(INSTR)); + std::memmove(&instr[c], &instr[c+1], (MAX_INSTR-2-c) * sizeof(INSTR)); clearinstr(MAX_INSTR-2); - for (d = 0; d < MAX_PATT; d++) + for (int d = 0; d < MAX_PATT; d++) { - for (e = 0; e < pattlen[d]; e++) + for (int e = 0; e < pattlen[d]; e++) { if ((pattern[d][e*4+1] > c) && (pattern[d][e*4+1] != MAX_INSTR-1)) pattern[d][e*4+1]--; @@ -1865,24 +1858,16 @@ void optimizeeverything(int oi, int ot) if (ot) { - for (c = 0; c < MAX_TABLES; c++) optimizetable(c); + for (int c = 0; c < MAX_TABLES; c++) optimizetable(c); } } -void mergesong(void) +void mergesong() { - int c; - char ident[4]; - FILE *handle; - int songbase; - int pattbase; - int instrbase; - int tablebase[MAX_TABLES]; - // Determine amount of patterns & instruments countpatternlengths(); highestusedinstr = 0; - for (c = 1; c < MAX_INSTR; c++) + for (int c = 1; c < MAX_INSTR; c++) { if ((instr[c].ad) || (instr[c].sr) || (instr[c].ptr[0]) || (instr[c].ptr[1]) || (instr[c].ptr[2]) || (instr[c].vibdelay) || (instr[c].ptr[3])) @@ -1892,7 +1877,7 @@ void mergesong(void) } // Determine amount of songs - c = MAX_SONGS - 1; + int c = MAX_SONGS - 1; if (numsids == 1) { for (;;) @@ -1916,40 +1901,40 @@ void mergesong(void) } } - pattbase = highestusedpattern + 1; - instrbase = highestusedinstr; - songbase = c + 1; + int pattbase = highestusedpattern + 1; + int instrbase = highestusedinstr; + int songbase = c + 1; - for (c = 0; c < MAX_TABLES; c++) + int tablebase[MAX_TABLES]; + + for (int c = 0; c < MAX_TABLES; c++) { tablebase[c] = gettablelen(c); } - handle = fopen(songfilename, "rb"); + char ident[4]; + + FILE *handle = std::fopen(songfilename, "rb"); if (handle) { - fread(ident, 4, 1, handle); - if ((!memcmp(ident, "GTS3", 4)) || (!memcmp(ident, "GTS4", 4)) || (!memcmp(ident, "GTS5", 4))) + std::fread(ident, 4, 1, handle); + if ((!std::memcmp(ident, "GTS3", 4)) || (!std::memcmp(ident, "GTS4", 4)) || (!std::memcmp(ident, "GTS5", 4))) { - int d; - int e; int length; - int amount; int loadsize; - int channelstoload; // Skip infotexts - fseek(handle, sizeof songname + sizeof authorname + sizeof copyrightname, SEEK_CUR); + std::fseek(handle, sizeof songname + sizeof authorname + sizeof copyrightname, SEEK_CUR); // Read songorderlists - channelstoload = determinechannels(handle); - amount = fread8(handle); + int channelstoload = determinechannels(handle); + int amount = fread8(handle); if (amount + songbase > MAX_SONGS) goto ABORT; - for (d = 0; d < amount; d++) + for (int d = 0; d < amount; d++) { - for (c = 0; c < channelstoload; c++) + for (int c = 0; c < channelstoload; c++) { length = fread8(handle); loadsize = length; @@ -1960,7 +1945,7 @@ void mergesong(void) } else if (numsids == 2) { - fread( + std::fread( songorder_stereo[songbase + d][c], loadsize, 1, @@ -1968,7 +1953,7 @@ void mergesong(void) ); } // Remap patterns - for (e = 0; e < loadsize - 1; e++) + for (int e = 0; e < loadsize - 1; e++) { if (numsids == 1) { @@ -1987,7 +1972,7 @@ void mergesong(void) amount = fread8(handle); if (amount + instrbase > MAX_INSTR) goto ABORT; - for (c = 1; c <= amount; c++) + for (int c = 1; c <= amount; c++) { instr[c + instrbase].ad = fread8(handle); instr[c + instrbase].sr = fread8(handle); @@ -2006,18 +1991,18 @@ void mergesong(void) instr[c + instrbase].vibdelay = fread8(handle); instr[c + instrbase].gatetimer = fread8(handle); instr[c + instrbase].firstwave = fread8(handle); - fread(&instr[c + instrbase].name, MAX_INSTRNAMELEN, 1, handle); + std::fread(&instr[c + instrbase].name, MAX_INSTRNAMELEN, 1, handle); } // Read tables - for (c = 0; c < MAX_TABLES; c++) + for (int c = 0; c < MAX_TABLES; c++) { loadsize = fread8(handle); if (loadsize + tablebase[c] > MAX_TABLELEN) goto ABORT; - fread(<able[c][tablebase[c]], loadsize, 1, handle); - fread(&rtable[c][tablebase[c]], loadsize, 1, handle); + std::fread(<able[c][tablebase[c]], loadsize, 1, handle); + std::fread(&rtable[c][tablebase[c]], loadsize, 1, handle); // Remap jumps and tablecommands - for (d = tablebase[c]; d < tablebase[c] + loadsize; d++) + for (int d = tablebase[c]; d < tablebase[c] + loadsize; d++) { if (ltable[c][d] == 0xff && rtable[c][d] > 0) rtable[c][d] += tablebase[c]; @@ -2040,12 +2025,12 @@ void mergesong(void) if (amount + pattbase > MAX_PATT) goto ABORT; - for (c = 0; c < amount; c++) + for (int c = 0; c < amount; c++) { length = fread8(handle) * 4; - fread(pattern[c + pattbase], length, 1, handle); + std::fread(pattern[c + pattbase], length, 1, handle); // Remap pattern instruments and commands - for (d = 0; d < length; d += 4) + for (int d = 0; d < length; d += 4) { if (pattern[c + pattbase][d + 1] > 0) pattern[c + pattbase][d + 1] += instrbase; @@ -2063,26 +2048,25 @@ void mergesong(void) } } - ABORT: - fclose(handle); +ABORT: + std::fclose(handle); countpatternlengths(); songchange(); } int determinechannels(FILE* handle) { - int returnpos = ftell(handle); - int c, d; + int returnpos = std::ftell(handle); int songs = fread8(handle); unsigned char songbuffer[257]; - for (d = 0; d < songs; d++) + for (int d = 0; d < songs; d++) { - for (c = 0; c < MAX_CHN; c++) + for (int c = 0; c < MAX_CHN; c++) { int loadsize = fread8(handle); loadsize++; - memset(songbuffer, 0, 257); + std::memset(songbuffer, 0, 257); fread(songbuffer, loadsize, 1, handle); // Check that each track of each song has a valid endmark. @@ -2095,6 +2079,6 @@ int determinechannels(FILE* handle) } } - fseek(handle, returnpos, SEEK_SET); + std::fseek(handle, returnpos, SEEK_SET); return MAX_CHN; } diff --git a/src/gsong.h b/src/song.h similarity index 55% rename from src/gsong.h rename to src/song.h index 42bee12..fef8c7c 100644 --- a/src/gsong.h +++ b/src/song.h @@ -1,7 +1,25 @@ -#ifndef GSONG_H -#define GSONG_H +/* + * LoadTracker + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ -#ifndef GSONG_C +#ifndef SONG_H +#define SONG_H + +#ifndef SONG_C extern INSTR instr[MAX_INSTR]; extern unsigned char ltable[MAX_TABLES][MAX_TABLELEN]; extern unsigned char rtable[MAX_TABLES][MAX_TABLELEN]; @@ -14,8 +32,6 @@ extern char copyrightname[MAX_STR]; extern int pattlen[MAX_PATT]; extern int songlen[MAX_SONGS][MAX_CHN_MONO]; extern int songlen_stereo[MAX_SONGS][MAX_CHN]; -extern int highestusedpattern; -extern int highestusedinstr; #endif void loadsong(void); @@ -30,7 +46,6 @@ void clearpattern(int p); int insertpattern(int p); void deletepattern(int p); void findusedpatterns(void); -void findduplicatepatterns(void); void optimizeeverything(int oi, int ot); #endif diff --git a/src/gsound.c b/src/sound.cpp similarity index 63% rename from src/gsound.c rename to src/sound.cpp index 882c479..20b94b8 100644 --- a/src/gsound.c +++ b/src/sound.cpp @@ -1,10 +1,31 @@ /* - * ============================================================================= - * sound routines - * ============================================================================= + * LoadTracker + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#define GSOUND_C +// ============================================================================= +// sound routines +// ============================================================================= + +#define SOUND_C + +#include "loadtrk.h" +#include "bme_snd.h" + +#include #ifdef __WIN32__ #include @@ -14,17 +35,23 @@ # include #endif -#include "loadtrk.h" +#include +#include + +#define MINMIXRATE 11025 +#define MAXMIXRATE 48000 + +#define MIXBUFFERSIZE 65536 // General / reSID output int playspeed; -int useexsid = 0; -int initted = 0; +bool useexsid = false; +bool initted = false; unsigned framerate = PALFRAMERATE; -Sint16 *buffer = NULL; -Sint16 *lbuffer = NULL; -Sint16 *rbuffer = NULL; -FILE *writehandle = NULL; +Sint16 *buffer = nullptr; +Sint16 *lbuffer = nullptr; +Sint16 *rbuffer = nullptr; +FILE *writehandle = nullptr; SDL_TimerID timer = 0; void sound_playrout(void); @@ -32,7 +59,7 @@ void sound_mixer(Sint32 *dest, unsigned samples); Uint32 sound_timer(void *userdata, SDL_TimerID timerID, Uint32 interval); #ifdef USE_EXSID -void* exsidfd = NULL; +void* exsidfd = nullptr; unsigned exsidDelay = 0; #endif @@ -101,19 +128,18 @@ int sound_init(unsigned mr, unsigned writer, unsigned m, unsigned ntsc, exsidDelay /= framerate; exsidDelay -= SIDWRITEDELAY*NUMSIDREGS; - useexsid = 1; - timer = SDL_AddTimer(1000 / framerate, sound_timer, NULL); + useexsid = true; + timer = SDL_AddTimer(1000 / framerate, sound_timer, nullptr); goto SOUNDOK; } #endif - if (!buffer) buffer = (Sint16*)malloc(MIXBUFFERSIZE * sizeof(Sint16)); - if (!lbuffer) lbuffer = (Sint16*)malloc(MIXBUFFERSIZE * sizeof(Sint16)); - if (!rbuffer) rbuffer = (Sint16*)malloc(MIXBUFFERSIZE * sizeof(Sint16)); - if ((!buffer) || (!lbuffer) || (!rbuffer)) return 0; + if (!buffer) buffer = new Sint16[MIXBUFFERSIZE * sizeof(Sint16)]; + if (!lbuffer) lbuffer = new Sint16[MIXBUFFERSIZE * sizeof(Sint16)]; + if (!rbuffer) rbuffer = new Sint16[MIXBUFFERSIZE * sizeof(Sint16)]; if (writer) - writehandle = fopen("sidaudio.raw", "wb"); + writehandle = std::fopen("sidaudio.raw", "wb"); playspeed = mr; if (playspeed < MINMIXRATE) playspeed = MINMIXRATE; @@ -133,15 +159,17 @@ int sound_init(unsigned mr, unsigned writer, unsigned m, unsigned ntsc, snd_player = &sound_playrout; snd_setcustommixer(sound_mixer); +#ifdef USE_EXSID SOUNDOK: - initted = 1; +#endif + initted = true; return 1; } void sound_uninit(void) { if (!initted) return; - initted = 0; + initted = false; // Apparently a delay is needed to make sure the sound timer thread is // not mixing stuff anymore, and we can safely delete related structures @@ -153,40 +181,40 @@ void sound_uninit(void) } else { - snd_setcustommixer(NULL); - snd_player = NULL; + snd_setcustommixer(nullptr); + snd_player = nullptr; } if (writehandle) { - fclose(writehandle); - writehandle = NULL; + std::fclose(writehandle); + writehandle = nullptr; } if (buffer) { - free(buffer); - buffer = NULL; + delete [] buffer; + buffer = nullptr; } if (lbuffer) { - free(lbuffer); - lbuffer = NULL; + delete [] lbuffer; + lbuffer = nullptr; } if (rbuffer) { - free(rbuffer); - rbuffer = NULL; + delete [] rbuffer; + rbuffer = nullptr; } #ifdef USE_EXSID if (useexsid) { - if (exsidfd != NULL) + if (exsidfd != nullptr) { - for (c = 0; c < NUMSIDREGS; c++) + for (int c = 0; c < NUMSIDREGS; c++) { exSID_clkdwrite(exsidfd, SIDWRITEDELAY, c, 0x00); } @@ -196,7 +224,7 @@ void sound_uninit(void) exSID_exit(exsidfd); exSID_free(exsidfd); - exsidfd = NULL; + exsidfd = nullptr; } #endif } @@ -208,7 +236,7 @@ Uint32 sound_timer(void *userdata, SDL_TimerID timerID, Uint32 interval) return interval; } -void sound_playrout(void) +void sound_playrout() { if (numsids == 1) { @@ -223,7 +251,7 @@ void sound_playrout(void) if (useexsid) { exSID_delay(exsidfd, exsidDelay); - for (c = 0; c < NUMSIDREGS; c++) + for (int c = 0; c < NUMSIDREGS; c++) { unsigned o = sid_getorder(c); exSID_clkdwrite(exsidfd, SIDWRITEDELAY, o, sidreg[o]); @@ -234,8 +262,6 @@ void sound_playrout(void) void sound_mixer(Sint32 *dest, unsigned samples) { - int c; - if (!initted) return; if (samples > MIXBUFFERSIZE) return; @@ -245,10 +271,10 @@ void sound_mixer(Sint32 *dest, unsigned samples) sid_fillbuffer(buffer, samples); if (writehandle) { - fwrite(buffer, samples * sizeof(Uint16), 1, writehandle); + std::fwrite(buffer, samples * sizeof(Uint16), 1, writehandle); } - for (c = 0; c < samples; c++) + for (unsigned c = 0; c < samples; c++) { dest[c] = buffer[c]; } @@ -258,15 +284,15 @@ void sound_mixer(Sint32 *dest, unsigned samples) sid_fillbuffer_stereo(lbuffer, rbuffer, samples); if (writehandle) { - for (c = 0; c < samples; c++) + for (unsigned c = 0; c < samples; c++) { - fwrite(&lbuffer[c], sizeof(Sint16), 1, writehandle); - fwrite(&rbuffer[c], sizeof(Sint16), 1, writehandle); + std::fwrite(&lbuffer[c], sizeof(Sint16), 1, writehandle); + std::fwrite(&rbuffer[c], sizeof(Sint16), 1, writehandle); } } if (monomode) { - for (c = 0; c < samples; c++) + for (unsigned c = 0; c < samples; c++) { dest[c*2] = lbuffer[c] / 2 + rbuffer[c] / 2; dest[c*2+1] = dest[c*2]; @@ -274,7 +300,7 @@ void sound_mixer(Sint32 *dest, unsigned samples) } else { - for (c = 0; c < samples; c++) + for (unsigned c = 0; c < samples; c++) { dest[c*2] = lbuffer[c] * panning + rbuffer[c] * (1-panning); dest[c*2+1] = rbuffer[c] * panning + lbuffer[c] * (1-panning); diff --git a/src/sound.h b/src/sound.h new file mode 100644 index 0000000..e126714 --- /dev/null +++ b/src/sound.h @@ -0,0 +1,34 @@ +/* + * LoadTracker + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef SOUND_H +#define SOUND_H + +#define DEFAULTMIXRATE 48000 + +#define PALFRAMERATE 50 +#define PALCLOCKRATE 985248 +#define NTSCFRAMERATE 60 +#define NTSCCLOCKRATE 1022727 + +int sound_init(unsigned mr, unsigned writer, unsigned m, unsigned ntsc, + unsigned multiplier, unsigned interpolate, unsigned customclockrate, + unsigned exsid, float filterbias, unsigned combwaves); +void sound_uninit(); + +#endif diff --git a/src/gtable.c b/src/table.cpp similarity index 88% rename from src/gtable.c rename to src/table.cpp index e92475c..49c6d42 100644 --- a/src/gtable.c +++ b/src/table.cpp @@ -1,12 +1,32 @@ /* - * ============================================================================= - * table editor - * ============================================================================= + * LoadTracker + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#define GTABLE_C +// ============================================================================= +// table editor +// ============================================================================= + +#define TABLE_C #include "loadtrk.h" +#include "bme_main.h" + +#include +#include unsigned char ltablecopybuffer[MAX_TABLELEN]; unsigned char rtablecopybuffer[MAX_TABLELEN]; @@ -21,10 +41,13 @@ int etmarknum = -1; int etmarkstart; int etmarkend; -void tablecommands(void) -{ - int c; +void tableup(); +void tabledown(); +void inserttable(int num, int pos, int mode); +void deletetable(int num, int pos); +void tablecommands() +{ switch(rawkey) { case KEY_Q: @@ -51,7 +74,7 @@ void tablecommands(void) rtable[etnum][etpos] = speed & 0xff; } break; - + case KEY_W: if ((shiftpressed) && (etnum == STBL)) { @@ -72,7 +95,7 @@ void tablecommands(void) rtable[etnum][etpos] = speed; } break; - + case KEY_S: if ((shiftpressed) && (etnum == STBL)) { @@ -127,17 +150,17 @@ void tablecommands(void) case KEY_HOME: while (etpos != 0) tableup(); break; - + case KEY_END: while (etpos != MAX_TABLELEN-1) tabledown(); break; case KEY_PGUP: - for (c = 0; c < PGUPDNREPEAT; c++) tableup(); + for (int c = 0; c < PGUPDNREPEAT; c++) tableup(); break; case KEY_PGDN: - for (c = 0; c < PGUPDNREPEAT; c++) tabledown(); + for (int c = 0; c < PGUPDNREPEAT; c++) tabledown(); break; case KEY_UP: @@ -157,7 +180,7 @@ void tablecommands(void) int d = 0; if (etmarkstart <= etmarkend) { - for (c = etmarkstart; c <= etmarkend; c++) + for (int c = etmarkstart; c <= etmarkend; c++) { ltablecopybuffer[d] = ltable[etmarknum][c]; rtablecopybuffer[d] = rtable[etmarknum][c]; @@ -171,7 +194,7 @@ void tablecommands(void) } else { - for (c = etmarkend; c <= etmarkstart; c++) + for (int c = etmarkend; c <= etmarkstart; c++) { ltablecopybuffer[d] = ltable[etmarknum][c]; rtablecopybuffer[d] = rtable[etmarknum][c]; @@ -194,7 +217,7 @@ void tablecommands(void) { if (tablecopyrows) { - for (c = 0; c < tablecopyrows; c++) + for (int c = 0; c < tablecopyrows; c++) { ltable[etnum][etpos] = ltablecopybuffer[c]; rtable[etnum][etpos] = rtablecopybuffer[c]; @@ -244,7 +267,6 @@ void tablecommands(void) case KEY_L: if (etnum == PTBL) { - int c; int currentpulse = -1; int targetpulse = ltable[etnum][etpos] << 4; int speed = rtable[etnum][etpos]; @@ -253,6 +275,7 @@ void tablecommands(void) if (!speed) break; + int c; // Follow the chain of pulse commands backwards to the nearest set command so we know what current pulse is for (c = etpos-1; c >= 0; c--) { @@ -277,7 +300,7 @@ void tablecommands(void) } } - time = abs(targetpulse - currentpulse) / speed; + time = std::abs(targetpulse - currentpulse) / speed; if (speed < 128) steps = (time + 126) / 127; else @@ -292,7 +315,7 @@ void tablecommands(void) while (time) { - if (abs(speed) < 128) + if (std::abs(speed) < 128) { if (time < 127) ltable[etnum][etpos] = time; else ltable[etnum][etpos] = 127; @@ -312,7 +335,6 @@ void tablecommands(void) } if (etnum == FTBL) { - int c; int currentfilter = -1; int targetfilter = ltable[etnum][etpos]; int speed = rtable[etnum][etpos] & 0x7f; @@ -321,6 +343,7 @@ void tablecommands(void) if (!speed) break; + int c; // Follow the chain of filter commands backwards to the nearest set command so we know what current pulse is for (c = etpos-1; c >= 0; c--) { @@ -343,7 +366,7 @@ void tablecommands(void) } } - time = abs(targetfilter - currentfilter) / speed; + time = std::abs(targetfilter - currentfilter) / speed; steps = (time + 126) / 127; if (!steps) break; if (etpos + steps > MAX_TABLELEN) break; @@ -492,7 +515,7 @@ void tablecommands(void) return; } break; - + case KEY_APOST2: if (shiftpressed) { @@ -545,16 +568,14 @@ void tablecommands(void) void deletetable(int num, int pos) { - int c, d; - // Shift tablepointers in instruments - for (c = 1; c < MAX_INSTR; c++) + for (int c = 1; c < MAX_INSTR; c++) { if ((instr[c].ptr[num]-1) > pos) instr[c].ptr[num]--; } // Shift tablepointers in wavetable commands - for (c = 0; c < MAX_TABLELEN; c++) + for (int c = 0; c < MAX_TABLELEN; c++) { if ((ltable[WTBL][c] >= WAVECMD) && (ltable[WTBL][c] <= WAVELASTCMD)) { @@ -578,9 +599,9 @@ void deletetable(int num, int pos) } // Shift tablepointers in patterns - for (c = 0; c < MAX_PATT; c++) + for (int c = 0; c < MAX_PATT; c++) { - for (d = 0; d <= MAX_PATTROWS; d++) + for (int d = 0; d <= MAX_PATTROWS; d++) { if (num < STBL) { @@ -601,7 +622,7 @@ void deletetable(int num, int pos) } // Shift jumppointers in the table itself - for (c = 0; c < MAX_TABLELEN; c++) + for (int c = 0; c < MAX_TABLELEN; c++) { if (num != STBL) { @@ -610,7 +631,7 @@ void deletetable(int num, int pos) } } - for (c = pos; c < MAX_TABLELEN; c++) + for (int c = pos; c < MAX_TABLELEN; c++) { if (c+1 < MAX_TABLELEN) { @@ -627,10 +648,8 @@ void deletetable(int num, int pos) void inserttable(int num, int pos, int mode) { - int c, d; - // Shift tablepointers in instruments - for (c = 1; c < MAX_INSTR; c++) + for (int c = 1; c < MAX_INSTR; c++) { if (!mode) { @@ -643,7 +662,7 @@ void inserttable(int num, int pos, int mode) } // Shift tablepointers in wavetable commands - for (c = 0; c < MAX_TABLELEN; c++) + for (int c = 0; c < MAX_TABLELEN; c++) { if ((ltable[WTBL][c] >= WAVECMD) && (ltable[WTBL][c] <= WAVELASTCMD)) { @@ -682,9 +701,9 @@ void inserttable(int num, int pos, int mode) // Shift tablepointers in patterns - for (c = 0; c < MAX_PATT; c++) + for (int c = 0; c < MAX_PATT; c++) { - for (d = 0; d <= MAX_PATTROWS; d++) + for (int d = 0; d <= MAX_PATTROWS; d++) { if (num < STBL) { @@ -721,7 +740,7 @@ void inserttable(int num, int pos, int mode) // Shift jumppointers in the table itself if (num != STBL) { - for (c = 0; c < MAX_TABLELEN; c++) + for (int c = 0; c < MAX_TABLELEN; c++) { if (ltable[num][c] == 0xff) { @@ -737,7 +756,7 @@ void inserttable(int num, int pos, int mode) } } - for (c = MAX_TABLELEN-1; c >= pos; c--) + for (int c = MAX_TABLELEN-1; c >= pos; c--) { if (c > pos) { @@ -763,7 +782,6 @@ void inserttable(int num, int pos, int mode) int gettablelen(int num) { int c; - for (c = MAX_TABLELEN-1; c >= 0; c--) { if (ltable[num][c] | rtable[num][c]) break; @@ -773,11 +791,10 @@ int gettablelen(int num) int gettablepartlen(int num, int pos) { - int c; - if (pos < 0) return 0; if (num == STBL) return 1; + int c; for (c = pos; c < MAX_TABLELEN; c++) { if (ltable[num][c] == 0xff) @@ -791,13 +808,11 @@ int gettablepartlen(int num, int pos) void optimizetable(int num) { - int c,d; - - memset(tableused, 0, sizeof tableused); + std::memset(tableused, 0, sizeof tableused); - for (c = 0; c < MAX_PATT; c++) + for (int c = 0; c < MAX_PATT; c++) { - for (d = 0; d < pattlen[c]; d++) + for (int d = 0; d < pattlen[c]; d++) { if ((pattern[c][d*4+2] >= CMD_SETWAVEPTR) && (pattern[c][d*4+2] <= CMD_SETFILTERPTR)) exectable(pattern[c][d*4+2] - CMD_SETWAVEPTR, pattern[c][d*4+3]); @@ -808,21 +823,21 @@ void optimizetable(int num) } } - for (c = 0; c < MAX_INSTR; c++) + for (int c = 0; c < MAX_INSTR; c++) { - for (d = 0; d < MAX_TABLES; d++) + for (int d = 0; d < MAX_TABLES; d++) { exectable(d, instr[c].ptr[d]); } } - for (c = 0; c < MAX_TABLELEN; c++) + for (int c = 0; c < MAX_TABLELEN; c++) { if (tableused[WTBL][c+1]) { if ((ltable[WTBL][c] >= WAVECMD) && (ltable[WTBL][c] <= WAVELASTCMD)) { - d = -1; + int d = -1; switch(ltable[WTBL][c] - WAVECMD) { @@ -847,6 +862,7 @@ void optimizetable(int num) } } + int c; for (c = MAX_TABLELEN-1; c >= 0; c--) { if ((ltable[num][c]) || (rtable[num][c])) break; @@ -859,7 +875,6 @@ void optimizetable(int num) int makespeedtable(unsigned data, int mode, int makenew) { - int c; unsigned char l = 0, r = 0; if (!data) return -1; @@ -885,7 +900,7 @@ int makespeedtable(unsigned data, int mode, int makenew) l = (data << 2) >> 8; r = (data << 2) & 0xff; break; - + case MST_RAW: r = data & 0xff; l = data >> 8; @@ -894,14 +909,14 @@ int makespeedtable(unsigned data, int mode, int makenew) if (makenew == 0) { - for (c = 0; c < MAX_TABLELEN; c++) + for (int c = 0; c < MAX_TABLELEN; c++) { if ((ltable[STBL][c] == l) && (rtable[STBL][c] == r)) return c; } } - for (c = 0; c < MAX_TABLELEN; c++) + for (int c = 0; c < MAX_TABLELEN; c++) { if ((!ltable[STBL][c]) && (!rtable[STBL][c])) { @@ -917,10 +932,9 @@ int makespeedtable(unsigned data, int mode, int makenew) void deleteinstrtable(int i) { - int c,d; - int eraseok = 1; + bool eraseok = true; - for (c = 0; c < MAX_TABLES; c++) + for (int c = 0; c < MAX_TABLES; c++) { if (instr[i].ptr[c]) { @@ -928,12 +942,12 @@ void deleteinstrtable(int i) int len = gettablepartlen(c, pos); // Check that this table area isn't used by another instrument - for (d = 1; d < MAX_INSTR; d++) + for (int d = 1; d < MAX_INSTR; d++) { if ((d != i) && (instr[d].ptr[c])) { int cmppos = instr[d].ptr[c]-1; - if ((cmppos >= pos) && (cmppos < pos+len)) eraseok = 0; + if ((cmppos >= pos) && (cmppos < pos+len)) eraseok = false; } } if (eraseok) @@ -962,6 +976,7 @@ void settableviewfirst(int num, int pos) etview[num] = pos; settableview(num, pos); } + void validatetableview(void) { if (etpos - etview[etnum] < 0) @@ -972,13 +987,11 @@ void validatetableview(void) // Table view lock? if (etlock) { - int c; - - for (c = 0; c < MAX_TABLES; c++) etview[c] = etview[etnum]; + for (int c = 0; c < MAX_TABLES; c++) etview[c] = etview[etnum]; } } -void tableup(void) +void tableup() { if (shiftpressed) { @@ -993,7 +1006,7 @@ void tableup(void) if (shiftpressed) etmarkend = etpos; } -void tabledown(void) +void tabledown() { if (shiftpressed) { @@ -1047,10 +1060,9 @@ void exectable(int num, int ptr) } } -int findfreespeedtable(void) +int findfreespeedtable() { - int c; - for (c = 0; c < MAX_TABLELEN; c++) + for (int c = 0; c < MAX_TABLELEN; c++) { if ((!ltable[STBL][c]) && (!rtable[STBL][c])) { diff --git a/src/table.h b/src/table.h new file mode 100644 index 0000000..b8eb779 --- /dev/null +++ b/src/table.h @@ -0,0 +1,52 @@ +/* + * LoadTracker + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef TABLE_H +#define TABLE_H + +#define MST_NOFINEVIB 0 +#define MST_FINEVIB 1 +#define MST_FUNKTEMPO 2 +#define MST_PORTAMENTO 3 +#define MST_RAW 4 + +#ifndef TABLE_C +extern int etview[MAX_TABLES]; +extern int etnum; +extern int etpos; +extern int etcolumn; +extern int etlock; +extern int etmarknum; +extern int etmarkstart; +extern int etmarkend; +#endif + +void tablecommands(); +int makespeedtable(unsigned data, int mode, int makenew); +void optimizetable(int num); +void deleteinstrtable(int i); +int gettablelen(int num); +int gettablepartlen(int num, int pos); +void gototable(int num, int pos); +void settableview(int num, int pos); +void settableviewfirst(int num, int pos); +void validatetableview(); +void exectable(int num, int ptr); +int findfreespeedtable(); + +#endif diff --git a/src/bme/dat2inc.c b/src/tools/dat2inc.cpp similarity index 55% rename from src/bme/dat2inc.c rename to src/tools/dat2inc.cpp index bda6c34..1986228 100644 --- a/src/bme/dat2inc.c +++ b/src/tools/dat2inc.cpp @@ -2,50 +2,45 @@ // Datafile -> C include file // -#include +#include +#include int main(int argc, char *argv[]) { - FILE *in; - FILE *out; - int length; - int c; - if (argc < 3) { printf("Usage: dat2inc \n\n"); - return 1; + return EXIT_FAILURE; } - in = fopen(argv[1], "rb"); + FILE *in = std::fopen(argv[1], "rb"); if (!in) { printf("Datafile open error!\n"); - return 1; + return EXIT_FAILURE; } - out = fopen(argv[2], "wt"); + FILE *out = std::fopen(argv[2], "wt"); if (!out) { printf("Includefile open error!\n"); - fclose(in); - return 1; + std::fclose(in); + return EXIT_FAILURE; } - fseek(in, 0, SEEK_END); - length = ftell(in); - fseek(in, 0, SEEK_SET); + std::fseek(in, 0, SEEK_END); + int length = ftell(in); + std::fseek(in, 0, SEEK_SET); fprintf(out, "unsigned char datafile[] = {\n"); - for (c = 0; c < length; c++) + for (int c = 0; c < length; c++) { if (c) - { + { fprintf(out, ", "); if (!(c % 10)) fprintf(out, "\n"); } fprintf(out, "0x%02x", fgetc(in)); } fprintf(out, "};\n"); - fclose(in); - fclose(out); - return 0; + std::fclose(in); + std::fclose(out); + return EXIT_SUCCESS; } - diff --git a/src/tools/datafile.cpp b/src/tools/datafile.cpp new file mode 100644 index 0000000..afae7d8 --- /dev/null +++ b/src/tools/datafile.cpp @@ -0,0 +1,157 @@ +// +// Datafile creator +// + +#include "bme_end.h" + +#include + +#include +#include +#include +#include +#include + +#define MAXFILES 16384 +#define MAXFILENAME 64 + +typedef struct +{ + uint32_t offset; + uint32_t length; + char name[13]; +} HEADER; + +static HEADER header[MAXFILES]; +static char fullname[MAXFILES][MAXFILENAME]; +static int files; + +bool addfile(HEADER *header, FILE *dest, char *name); + +int main(int argc, char **argv) +{ + if (argc < 3) + { + std::printf("Usage: DATAFILE \n\n" + "The purpose of this program is to gather many files into one datafile, like\n" + "usually seen in games. The files can be read by BME's IO functions after\n" + "opening the datafile first with io_opendatafile(). The filelistfile must\n" + "contain name (use only 8+3 chars) of each file on its own row, for example:\n\n" + "editor.spr\n" + "fonts.spr\n"); + return EXIT_SUCCESS; + } + FILE *listfile = std::fopen(argv[2], "rt"); + if (!listfile) + { + std::printf("ERROR: Couldn't open filelist.\n"); + return EXIT_FAILURE; + } + FILE *datafile = std::fopen(argv[1], "wb"); + if (!datafile) + { + std::printf("ERROR: Couldn't create datafile.\n"); + std::fclose(listfile); + return EXIT_FAILURE; + } + std::memset(&header[0], 0, MAXFILES * sizeof(HEADER)); + // Get names from list + for (;;) + { + char searchname[64]; + + if (std::fscanf(listfile, "%63s", searchname) == EOF) break; + FILE *test = std::fopen(searchname, "rb"); + if (test) + { + std::fclose(test); + + std::strcpy(fullname[files], searchname); + + int c; + for (c = std::strlen(fullname[files]) - 1; c >= 0; c--) + { + if (fullname[files][c] == '\\') + { + c++; + break; + } + } + if (c < 0) c = 0; + std::memset(header[files].name, 0, 13); + int d = 0; + while (fullname[files][c]) + { + header[files].name[d] = toupper(fullname[files][c]); + c++; + d++; + } + files++; + if (files == MAXFILES) break; + } + if (files == MAXFILES) break; + } + + std::fclose(listfile); + // Write datafile header + std::fwrite("DAT!", 4, 1, datafile); + fwritele32(datafile, files); + // Write incomplete fileheaders + for (int c = 0; c < files; c++) + { + fwritele32(datafile, header[c].offset); + fwritele32(datafile, header[c].length); + std::fwrite(header[c].name, 13, 1, datafile); + } + // Process each file + for (int c = 0; c < files; c++) + { + std::printf("Adding %s...\n", header[c].name); + if (!addfile(&header[c], datafile, fullname[c])) + { + std::printf("Terminating & deleting datafile...\n"); + std::fclose(datafile); + remove(argv[1]); + return EXIT_FAILURE; + } + } + // Seek back to start & write correct headers + std::fseek(datafile, sizeof files + 4, SEEK_SET); + for (int c = 0; c < files; c++) + { + fwritele32(datafile, header[c].offset); + fwritele32(datafile, header[c].length); + std::fwrite(header[c].name, 13, 1, datafile); + } + std::fclose(datafile); + std::printf("Everything OK!\n"); + return EXIT_SUCCESS; +} + +bool addfile(HEADER *header, FILE *dest, char *name) +{ + FILE *src = std::fopen(name, "rb"); + if (!src) + { + std::printf("ERROR: Couldn't open file %s\n", name); + return false; + } + header->offset = std::ftell(dest); + std::fseek(src, 0, SEEK_END); + header->length = std::ftell(src); + std::fseek(src, 0, SEEK_SET); + unsigned char *originalbuf = new (std::nothrow) unsigned char[header->length]; + if (!originalbuf) + { + std::printf("ERROR: No memory to load file!\n"); + std::fclose(src); + return false; + } + std::printf("* Loading file\n"); + std::fread(originalbuf, header->length, 1, src); + std::fclose(src); + std::printf("* Writing file (size was %d)\n", header->length); + std::fwrite(originalbuf, header->length, 1, dest); + delete [] originalbuf; + return true; +} diff --git a/src/ins2snd2.c b/src/tools/ins2snd2.cpp similarity index 80% rename from src/ins2snd2.c rename to src/tools/ins2snd2.cpp index 3bea903..99abd6e 100644 --- a/src/ins2snd2.c +++ b/src/tools/ins2snd2.cpp @@ -2,12 +2,13 @@ * GoatTracker V2.0 Instrument -> Sound effect convertor */ -#include -#include -#include -#include #include "bme_end.h" -#include "gcommon.h" +#include "common.h" + +#include +#include +#include +#include unsigned char swapnybbles(unsigned char n); void outputbyte(unsigned char c); @@ -15,12 +16,10 @@ void outputbyte(unsigned char c); int covert = 0; int binary = 0; int bytecount = 0; -FILE *handle; FILE *out; int main(int argc, char **argv) { - int c,d; int prevwave = 0xff; int currwave = 0; int fileok = 0; @@ -54,11 +53,11 @@ int main(int argc, char **argv) if (argc > 3) { - for (c = 3; c < argc; c++) + for (int c = 3; c < argc; c++) { if (((argv[c][0] == '-') || (argv[c][0] == '/')) && (strlen(argv[c]) > 1)) { - int ch = tolower(argv[c][1]); + int ch = std::tolower(argv[c][1]); switch(ch) { case 'b': @@ -74,19 +73,19 @@ int main(int argc, char **argv) } - handle = fopen(argv[1], "rb"); + FILE *handle = std::fopen(argv[1], "rb"); if (!handle) { printf("ERROR: Can't open instrumentfile\n"); return EXIT_FAILURE; } - memset(wavetable, 0, MAX_TABLELEN*2); - memset(pulsetable, 0, MAX_TABLELEN*2); - memset(filttable, 0, MAX_TABLELEN*2); + std::memset(wavetable, 0, MAX_TABLELEN*2); + std::memset(pulsetable, 0, MAX_TABLELEN*2); + std::memset(filttable, 0, MAX_TABLELEN*2); - fread(ident, 4, 1, handle); - if (!memcmp(ident, "GTI!", 4)) + std::fread(ident, 4, 1, handle); + if (!std::memcmp(ident, "GTI!", 4)) { fileok = 1; instr.ad = fread8(handle); @@ -97,10 +96,10 @@ int main(int argc, char **argv) fread8(handle); // Throw away pulse limit high fread8(handle); // Throw away filtersetting wavelen = fread8(handle); - fread(&instr.name, MAX_INSTRNAMELEN, 1, handle); - fread(wavetable, wavelen, 1, handle); + std::fread(&instr.name, MAX_INSTRNAMELEN, 1, handle); + std::fread(wavetable, wavelen, 1, handle); } - if (!memcmp(ident, "GTI2", 4)) + if (!std::memcmp(ident, "GTI2", 4)) { fileok = 1; instr.ad = fread8(handle); @@ -114,23 +113,23 @@ int main(int argc, char **argv) instr.firstwave = fread8(handle); fread(&instr.name, MAX_INSTRNAMELEN, 1, handle); wavelen = fread8(handle); - for (c = 0; c < wavelen; c++) + for (int c = 0; c < wavelen; c++) wavetable[c*2] = fread8(handle); - for (c = 0; c < wavelen; c++) + for (int c = 0; c < wavelen; c++) wavetable[c*2+1] = fread8(handle); pulselen = fread8(handle); - for (c = 0; c < pulselen; c++) + for (int c = 0; c < pulselen; c++) pulsetable[c*2] = fread8(handle); - for (c = 0; c < pulselen; c++) + for (int c = 0; c < pulselen; c++) pulsetable[c*2+1] = fread8(handle); filtlen = fread8(handle); - for (c = 0; c < filtlen; c++) + for (int c = 0; c < filtlen; c++) filttable[c*2] = fread8(handle); - for (c = 0; c < filtlen; c++) + for (int c = 0; c < filtlen; c++) filttable[c*2+1] = fread8(handle); pulse = (pulsetable[0] << 4) | (pulsetable[1] >> 4); } - if ((!memcmp(ident, "GTI3", 4)) || (!memcmp(ident, "GTI4", 4) || (!memcmp(ident, "GTI5", 4)))) + if ((!std::memcmp(ident, "GTI3", 4)) || (!std::memcmp(ident, "GTI4", 4) || (!std::memcmp(ident, "GTI5", 4)))) { fileok = 1; instr.ad = fread8(handle); @@ -142,25 +141,25 @@ int main(int argc, char **argv) instr.vibdelay = fread8(handle); instr.gatetimer = fread8(handle); instr.firstwave = fread8(handle); - fread(&instr.name, MAX_INSTRNAMELEN, 1, handle); + std::fread(&instr.name, MAX_INSTRNAMELEN, 1, handle); wavelen = fread8(handle); - for (c = 0; c < wavelen; c++) + for (int c = 0; c < wavelen; c++) wavetable[c*2] = fread8(handle); - for (c = 0; c < wavelen; c++) + for (int c = 0; c < wavelen; c++) wavetable[c*2+1] = fread8(handle); pulselen = fread8(handle); - for (c = 0; c < pulselen; c++) + for (int c = 0; c < pulselen; c++) pulsetable[c*2] = fread8(handle); - for (c = 0; c < pulselen; c++) + for (int c = 0; c < pulselen; c++) pulsetable[c*2+1] = fread8(handle); filtlen = fread8(handle); - for (c = 0; c < filtlen; c++) + for (int c = 0; c < filtlen; c++) filttable[c*2] = fread8(handle); - for (c = 0; c < filtlen; c++) + for (int c = 0; c < filtlen; c++) filttable[c*2+1] = fread8(handle); pulse = (pulsetable[0] << 4) | (pulsetable[1] >> 4); } - fclose(handle); + std::fclose(handle); if (!fileok) { printf("ERROR: File is not a GoatTracker instrument!\n"); @@ -178,7 +177,7 @@ int main(int argc, char **argv) return EXIT_FAILURE; } - d = 0; + int d = 0; outputbyte(instr.ad); d++; outputbyte(instr.sr); @@ -186,7 +185,7 @@ int main(int argc, char **argv) outputbyte(swapnybbles(pulse)); d++; - for (c = 0; c < MAX_TABLELEN; c++) + for (int c = 0; c < MAX_TABLELEN; c++) { if (wavetable[c*2] == 0xff) { @@ -221,11 +220,11 @@ int main(int argc, char **argv) } if (d > 255) { - fclose(out); + std::fclose(out); printf("ERROR: Sound effect exceeds 255 bytes\n"); return EXIT_FAILURE; } - fclose(out); + std::fclose(out); return EXIT_SUCCESS; } diff --git a/src/mod2sng.c b/src/tools/mod2sng.cpp similarity index 87% rename from src/mod2sng.c rename to src/tools/mod2sng.cpp index 492c31a..87858b0 100644 --- a/src/mod2sng.c +++ b/src/tools/mod2sng.cpp @@ -3,11 +3,12 @@ * Dedicated to T.M.R! */ -#include -#include -#include #include "bme_end.h" +#include +#include +#include + typedef struct { unsigned char note; @@ -64,11 +65,6 @@ int goatpatt = 0; int main(int argc, char **argv) { - FILE *in, *out; - int c, d; - unsigned char *srcptr; - NOTE *destptr; - if (argc < 3) { printf("Usage: mod2sng [channel] [transpose]\n" @@ -78,58 +74,58 @@ int main(int argc, char **argv) return EXIT_FAILURE; } - in = fopen(argv[1], "rb"); + FILE *in = std::fopen(argv[1], "rb"); if (!in) { printf("Source open error.\n"); return EXIT_FAILURE; } - out = fopen(argv[2], "wb"); + FILE *out = std::fopen(argv[2], "wb"); if (!out) { printf("Destination open error.\n"); - fclose(in); + std::fclose(in); return EXIT_FAILURE; } if (argc > 3) { - sscanf(argv[3], "%d", &leaveout); + std::sscanf(argv[3], "%d", &leaveout); leaveout--; if ((leaveout < 0) || (leaveout > 3)) { printf("Illegal channel number.\n"); - fclose(in); - fclose(out); + std::fclose(in); + std::fclose(out); return EXIT_FAILURE; } } if (argc > 4) { - sscanf(argv[4], "%d", &transpose); + std::sscanf(argv[4], "%d", &transpose); } - for (c = 0; c < 1084; c++) + for (int c = 0; c < 1084; c++) { modheader[c] = fread8(in); } orderlen = modheader[950]; - for (c = 0; c < 128; c++) + for (int c = 0; c < 128; c++) { if (modheader[952+c] > maxpatt) maxpatt = modheader[952+c]; } maxpatt++; - for (c = 0; c < maxpatt * 1024; c++) + for (int c = 0; c < maxpatt * 1024; c++) { modpatterns[c] = fread8(in); } - fclose(in); + std::fclose(in); // Convert patterns into easier-to-read format - destptr = modnotes; - srcptr = modpatterns; - for (c = 0; c < maxpatt * 256; c++) + NOTE *destptr = modnotes; + unsigned char *srcptr = modpatterns; + for (int c = 0; c < maxpatt * 256; c++) { // Note: FT2 saves the 13th bit of period into 5th bit of // samplenumber, and when loading it ofcourse cannot read @@ -162,11 +158,11 @@ int main(int argc, char **argv) } // Convert patterns into goatpatterns, and create orderlists - for (c = 0; c < 4; c++) + for (int c = 0; c < 4; c++) { if (c != leaveout) { - for (d = 0; d < orderlen; d++) + for (int d = 0; d < orderlen; d++) { int patt = modheader[952+d]; int e = 0; @@ -252,13 +248,12 @@ int main(int argc, char **argv) // No same pattern pattern if (e == goatpatt) { - int f; if (goatpatt >= 208) { printf("208 patterns exceeded!\n"); return EXIT_FAILURE; } - for (f = 0; f < 65; f++) + for (int f = 0; f < 65; f++) { goatnotes[goatpatt][f].note = tempnotes[f].note; goatnotes[goatpatt][f].command = tempnotes[f].command; @@ -271,17 +266,17 @@ int main(int argc, char **argv) goatchan++; } } - fwrite(ident, 4, 1, out); // Ident - fwrite(&modheader[0], 20, 1, out); // Infotexts - fwrite(zeroarray, 12, 1, out); - fwrite(zeroarray, 32, 1, out); - fwrite(zeroarray, 32, 1, out); + std::fwrite(ident, 4, 1, out); // Ident + std::fwrite(&modheader[0], 20, 1, out); // Infotexts + std::fwrite(zeroarray, 12, 1, out); + std::fwrite(zeroarray, 32, 1, out); + std::fwrite(zeroarray, 32, 1, out); fwrite8(out, 1); // Number of songs // Orderlist - for (c = 0; c < 3; c++) + for (int c = 0; c < 3; c++) { fwrite8(out, orderlen+1); - for (d = 0; d < orderlen; d++) + for (int d = 0; d < orderlen; d++) { fwrite8(out, orderlist[c][d]); } @@ -289,7 +284,7 @@ int main(int argc, char **argv) fwrite8(out, 0x0); // From beginning } // Instruments - for (c = 1; c < 32; c++) + for (int c = 1; c < 32; c++) { fwrite8(out, 0); fwrite8(out, 0); @@ -299,24 +294,24 @@ int main(int argc, char **argv) fwrite8(out, 0); fwrite8(out, 0); fwrite8(out, 2); - fwrite(&modheader[20+30*(c-1)], 15, 1, out); + std::fwrite(&modheader[20+30*(c-1)], 15, 1, out); fwrite8(out, 0); fwrite8(out, 0); fwrite8(out, 0); } // Patterns fwrite8(out, goatpatt); - for (c = 0; c < goatpatt; c++) + for (int c = 0; c < goatpatt; c++) { fwrite8(out, 65*3); - for (d = 0; d < 65; d++) + for (int d = 0; d < 65; d++) { fwrite8(out, goatnotes[c][d].note); fwrite8(out, goatnotes[c][d].command); fwrite8(out, goatnotes[c][d].data); } } - fclose(out); + std::fclose(out); printf("Converted successfully.\n"); return EXIT_SUCCESS; } diff --git a/src/sngspli2.c b/src/tools/sngspli2.cpp similarity index 86% rename from src/sngspli2.c rename to src/tools/sngspli2.cpp index 727c248..22e52a5 100644 --- a/src/sngspli2.c +++ b/src/tools/sngspli2.cpp @@ -2,12 +2,13 @@ * GoatTracker V2.xx pattern splitter */ -#include -#include -#include -#include #include "bme_end.h" -#include "gcommon.h" +#include "common.h" + +#include +#include +#include +#include #define MAX_SPLITS 16 @@ -58,7 +59,7 @@ int main(int argc, char **argv) "Default length is 16.\n"); return EXIT_FAILURE; } - if (!strcmp(argv[1], argv[2])) + if (!std::strcmp(argv[1], argv[2])) { printf("ERROR: Source and destination are not allowed to be the same."); return EXIT_FAILURE; @@ -66,7 +67,7 @@ int main(int argc, char **argv) if (argc >= 4) { - sscanf(argv[3], "%d", &targetlen); + std::sscanf(argv[3], "%d", &targetlen); if (targetlen < 1) targetlen = 1; if (targetlen > MAX_PATTROWS) targetlen = MAX_PATTROWS; } @@ -90,28 +91,27 @@ int main(int argc, char **argv) int processsong(void) { - int c,d,e; int splitsize; int songs; int dsl; dp = 0; // Destination patterns - for (c = 0; c <= highestusedpattern; c++) + for (int c = 0; c <= highestusedpattern; c++) { destpattsplits[c] = 0; splitsize = targetlen; while (pattlen[c] / splitsize > MAX_SPLITS) splitsize *= 2; if (pattlen[c] <= splitsize) splitsize = pattlen[c]; - d = 0; + int d = 0; while (d < pattlen[c]) { int remain = pattlen[c] - d; int splitfound = 0; // Check existing patterns for matches - for (e = 0; e < dp; e++) + for (int e = 0; e < dp; e++) { if ((destpattlen[e] <= remain) && (destpattlen[e] >= splitsize) && (!memcmp(&pattern[c][d*4], destpattern[e], destpattlen[e]*4))) { @@ -169,7 +169,7 @@ int processsong(void) // Now convert all songs // Determine amount of songs to be processed - c = 0; + int c = 0; for (;;) { if (c == MAX_SONGS) break; @@ -180,12 +180,12 @@ int processsong(void) } songs = c; - for (c = 0; c < songs; c++) + for (int c = 0; c < songs; c++) { - for (d = 0; d < MAX_CHN; d++) + for (int d = 0; d < MAX_CHN; d++) { dsl = 0; - for (e = 0; e <= songlen[c][d]+1; e++) + for (int e = 0; e <= songlen[c][d]+1; e++) { int pattnum = songorder[c][d][e]; @@ -239,23 +239,23 @@ int processsong(void) int destpatttbl = 0, destpatt = 0, destsong = 0; int srcpatttbl = 0, srcpatt = 0, srcsong = 0; - for (c = 0; c < MAX_SONGS; c++) + for (int c = 0; c < MAX_SONGS; c++) { if ((songlen[c][0]) && (songlen[c][1]) && (songlen[c][2])) { - for (d = 0; d < MAX_CHN; d++) + for (int d = 0; d < MAX_CHN; d++) { srcsong += songlen[c][d]+1; destsong += destsonglen[c][d]+1; } } } - for (c = 0; c < highestusedpattern; c++) + for (int c = 0; c < highestusedpattern; c++) { srcpatt += pattlen[c]*4 + 4; srcpatttbl += 2; } - for (c = 0; c < dp; c++) + for (int c = 0; c < dp; c++) { destpatt += destpattlen[c]*4 + 4; destpatttbl += 2; @@ -272,8 +272,6 @@ int processsong(void) int loadsong(char *name) { - int c; - FILE *srchandle = fopen(name, "rb"); if (srchandle) { @@ -285,7 +283,6 @@ int loadsong(char *name) if (tables) { - int d; unsigned char length; unsigned char amount; int loadbytes; @@ -298,9 +295,9 @@ int loadsong(char *name) // Read songorderlists amount = fread8(srchandle); - for (d = 0; d < amount; d++) + for (int d = 0; d < amount; d++) { - for (c = 0; c < MAX_CHN; c++) + for (int c = 0; c < MAX_CHN; c++) { length = fread8(srchandle); loadbytes = length; @@ -310,7 +307,7 @@ int loadsong(char *name) } // Read instruments highestusedinstr = fread8(srchandle); - for (c = 1; c <= highestusedinstr; c++) + for (int c = 1; c <= highestusedinstr; c++) { instr[c].ad = fread8(srchandle); instr[c].sr = fread8(srchandle); @@ -324,7 +321,7 @@ int loadsong(char *name) fread(&instr[c].name, MAX_INSTRNAMELEN, 1, srchandle); } // Read tables - for (c = 0; c < tables; c++) + for (int c = 0; c < tables; c++) { loadbytes = fread8(srchandle); fread(ltable[c], loadbytes, 1, srchandle); @@ -332,7 +329,7 @@ int loadsong(char *name) } // Read patterns amount = fread8(srchandle); - for (c = 0; c < amount; c++) + for (int c = 0; c < amount; c++) { length = fread8(srchandle); fread(pattern[c], length*4, 1, srchandle); @@ -349,12 +346,10 @@ int loadsong(char *name) int savesong(char *name) { FILE *handle; - int c; handle = fopen(name, "wb"); if (handle) { - int d; unsigned char length; unsigned char amount; int writebytes; @@ -368,7 +363,7 @@ int savesong(char *name) fwrite(copyrightname, sizeof copyrightname, 1, handle); // Determine amount of songs to be saved - c = 0; + int c = 0; for (;;) { if (c == MAX_SONGS) break; @@ -381,9 +376,9 @@ int savesong(char *name) fwrite8(handle, amount); // Write songorderlists - for (d = 0; d < amount; d++) + for (int d = 0; d < amount; d++) { - for (c = 0; c < MAX_CHN; c++) + for (int c = 0; c < MAX_CHN; c++) { length = destsonglen[d][c]+1; fwrite8(handle, length); @@ -433,21 +428,21 @@ int savesong(char *name) void countpatternlengths(void) { - int c, d, e; - highestusedpattern = 0; - for (c = 0; c < MAX_PATT; c++) + for (int c = 0; c < MAX_PATT; c++) { + int d; for (d = 0; d <= MAX_PATTROWS; d++) { if (pattern[c][d*4] == ENDPATT) break; } pattlen[c] = d; } - for (e = 0; e < MAX_SONGS; e++) + for (int e = 0; e < MAX_SONGS; e++) { - for (c = 0; c < MAX_CHN; c++) + for (int c = 0; c < MAX_CHN; c++) { + int d; for (d = 0; d < MAX_SONGLEN; d++) { if (songorder[e][c][d] >= LOOPSONG) break; @@ -464,20 +459,20 @@ void countpatternlengths(void) void countdestpatternlengths(void) { - int c, d, e; - - for (c = 0; c < MAX_PATT; c++) + for (int c = 0; c < MAX_PATT; c++) { + int d; for (d = 0; d <= MAX_PATTROWS; d++) { if (destpattern[c][d*4] == ENDPATT) break; } destpattlen[c] = d; } - for (e = 0; e < MAX_SONGS; e++) + for (int e = 0; e < MAX_SONGS; e++) { - for (c = 0; c < MAX_CHN; c++) + for (int c = 0; c < MAX_CHN; c++) { + int d; for (d = 0; d < MAX_SONGLEN; d++) { if (destsongorder[e][c][d] >= LOOPSONG) break; @@ -489,17 +484,14 @@ void countdestpatternlengths(void) void clearsong(void) { - int c; - - for (c = 0; c < MAX_CHN; c++) + for (int c = 0; c < MAX_CHN; c++) { - int d; - for (d = 0; d < MAX_SONGS; d++) + for (int d = 0; d < MAX_SONGS; d++) { destsonglen[d][c] = 0; destsongorder[d][c][0] = LOOPSONG; - memset(&songorder[d][c][0], 0, MAX_SONGLEN); + std::memset(&songorder[d][c][0], 0, MAX_SONGLEN); if (!d) { songorder[d][c][0] = c; @@ -513,23 +505,22 @@ void clearsong(void) } } } - memset(songname, 0, sizeof songname); - memset(authorname, 0, sizeof authorname); - memset(copyrightname, 0, sizeof copyrightname); + std::memset(songname, 0, sizeof songname); + std::memset(authorname, 0, sizeof authorname); + std::memset(copyrightname, 0, sizeof copyrightname); - for (c = 0; c < MAX_PATT; c++) + for (int c = 0; c < MAX_PATT; c++) { - int d; - memset(&pattern[c][0], 0, MAX_PATTROWS*4); - for (d = 0; d < MAX_PATTROWS; d++) pattern[c][d*4] = REST; - for (d = MAX_PATTROWS; d <= MAX_PATTROWS; d++) pattern[c][d*4] = ENDPATT; + std::memset(&pattern[c][0], 0, MAX_PATTROWS*4); + for (int d = 0; d < MAX_PATTROWS; d++) pattern[c][d*4] = REST; + for (int d = MAX_PATTROWS; d <= MAX_PATTROWS; d++) pattern[c][d*4] = ENDPATT; } - for (c = 0; c < MAX_INSTR; c++) + for (int c = 0; c < MAX_INSTR; c++) { - memset(&instr[c], 0, sizeof(INSTR)); + std::memset(&instr[c], 0, sizeof(INSTR)); } - memset(ltable, 0, sizeof ltable); - memset(rtable, 0, sizeof rtable); + std::memset(ltable, 0, sizeof ltable); + std::memset(rtable, 0, sizeof rtable); countpatternlengths(); } @@ -537,7 +528,6 @@ void clearsong(void) int gettablelen(int num) { int c; - for (c = MAX_TABLELEN-1; c >= 0; c--) { if (ltable[num][c] | rtable[num][c]) break;