From 5207c8f77a2769e773796921c0830298573a4eaf Mon Sep 17 00:00:00 2001 From: Scott Duensing Date: Thu, 25 Mar 2021 19:39:30 -0500 Subject: [PATCH] Moved pixel buffer routines into their own file for easier sharing between platforms. --- .gitignore | 3 + joeylib/joeylib.pro | 11 ++- joeylib/src/jPixBuf.c | 214 ++++++++++++++++++++++++++++++++++++++++++ joeylib/src/jPixBuf.h | 37 ++++++++ joeylib/src/jSDL2.c | 180 +---------------------------------- 5 files changed, 267 insertions(+), 178 deletions(-) create mode 100644 joeylib/src/jPixBuf.c create mode 100644 joeylib/src/jPixBuf.h diff --git a/.gitignore b/.gitignore index 3ccaf1c..fcc14bf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ *~ *.user +*.user.* joeylib/lib/ joeylib/src/SDL2/ joeylib/src/music @@ -13,3 +14,5 @@ assets/JoeyLib Logo.jpg assets/JoeyLib Logo.png *.stn *.img +*.sym +*.ntp diff --git a/joeylib/joeylib.pro b/joeylib/joeylib.pro index b486098..9d6d5a6 100644 --- a/joeylib/joeylib.pro +++ b/joeylib/joeylib.pro @@ -15,6 +15,7 @@ QMAKE_CFLAGS += \ -D_REENTRANT HEADERS += \ + src/jPixBuf.h \ src/joey.h \ src/stddclmr.h @@ -25,7 +26,9 @@ SOURCES += \ SDL12 { INCLUDEPATH += $$JOEY/sdks/linux/x64/include - SOURCES += src/jSDL12.c + SOURCES += \ + src/jPixBuf.c \ + src/jSDL12.c LIBS += \ -L$$JOEY/sdks/linux/x64/lib \ @@ -41,7 +44,9 @@ SDL12 { SDL2 { INCLUDEPATH += $$JOEY/sdks/linux/x64/include - SOURCES += src/jSDL2.c + SOURCES += \ + src/jPixBuf.c \ + src/jSDL2.c LIBS += \ -L$$JOEY/sdks/linux/x64/lib \ @@ -57,7 +62,7 @@ SDL2 { } OTHER_FILES += \ - src/jBlankcd ..c \ + src/jBlank.c \ src/jAmiga.c \ src/jIIgs.c \ src/jIIgs.asm \ diff --git a/joeylib/src/jPixBuf.c b/joeylib/src/jPixBuf.c new file mode 100644 index 0000000..6a5c70f --- /dev/null +++ b/joeylib/src/jPixBuf.c @@ -0,0 +1,214 @@ +/* + * JoeyLib + * Copyright (C) 2018-2019 Scott Duensing + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. +*/ + + +#include + +#include "jPixBuf.h" + + +#ifdef DMALLOC +#define DMALLOC_FUNC_CHECK +#include "dmalloc.h" +#endif + + +jlImgT *_jlBackingStore = NULL; // 4 bit representation + + +void jlDrawBlit8x8(jlSurfaceT source, jint16 sx, jint16 sy, jint16 tx, jint16 ty) { + int o1; + int o2; + int x; + int y; + jlPixelPairT *pixels = (jlPixelPairT *)source; + jlPixelPairT *target = (jlPixelPairT *)_jlDrawTargetActual; + + if (pixels == NULL) { + pixels = (jlPixelPairT *)_jlBackingStore->pixels; + } + + if (jlUtilIsOdd(sx)) sx--; // sx must be even because there are two pixels per byte + o1 = sy * 160 + (int)(sx * 0.5); // This is in pixels... + + if (jlUtilIsOdd(tx)) tx--; // tx must be even because there are two pixels per byte + o2 = ty * 160 + (int)(tx * 0.5); // This is in pixels... + + for (y=0; y<8; y++) { + for (x=0; x<4; x++) { + target[o2++] = pixels[o1++]; + } + o1 += 156; + o2 += 156; + } +} + + +void jlDrawBlit8x8a(jlSurfaceT source, jlStnT *stencil, jint16 sx, jint16 sy, jint16 tx, jint16 ty) { + int so; // Source Pixel Offset + int to; // Target Pixel Offset + int i; + int x; + int y; + int pos; + jlPixelPairT s; // Source Pixel + jlPixelPairT t; // Target Pixel + jlPixelPairT *pixels = (jlPixelPairT *)source; + jlPixelPairT *target = (jlPixelPairT *)_jlDrawTargetActual; + + if (pixels == NULL) { + pixels = (jlPixelPairT *)_jlBackingStore->pixels; + } + + if (jlUtilIsOdd(sx)) sx--; // sx must be even because there are two pixels per byte + so = sy * 160 + (int)(sx * 0.5); // This is in pixels... + + if (jlUtilIsOdd(tx)) tx--; // tx must be even because there are two pixels per byte + to = ty * 160 + (int)(tx * 0.5); // This is in pixels... + + // Color = <-- 40 tiles, 80 bytes, 160 pixels --> + // Stencil = <-- 20 bytes, 160 bits --> + + for (y=0; y<8; y++) { + for (x=0; x<4; x++) { + + t = target[to]; + s = pixels[so++]; + + i = ((sy + y) * 320 + (sx + x * 2)); + pos = 7 - (i % 8); + i /= 8; + + //***FIX*** Another endian order issue. Left & Right are swapped. + if ((stencil->pixels[i] & (1 << pos)) != 0) { + t.r = s.r; + } + + i = ((sy + y) * 320 + (sx + x * 2 + 1)); + pos = 7 - (i % 8); + i /= 8; + + if ((stencil->pixels[i] & (1 << pos)) != 0) { + t.l = s.l; + } + + target[to++] = t; + } + so += 156; + to += 156; + } +} + + +void jlDrawClear(void) { + jlPixelPairT *target = (jlPixelPairT *)_jlDrawTargetActual; + memset(target, _jlDrawColorNibbles, 32000); +} + + +byte jlDrawPixelGet(jint16 x, jint16 y) { + jlPixelPairT *target = (jlPixelPairT *)_jlDrawTargetActual; + int p = x / 2 + y * 160; + return (jlUtilIsOdd(x) ? target[p].l : target[p].r); +} + + +void jlDrawPixelSet(jint16 x, jint16 y) { + jlPixelPairT *target = (jlPixelPairT *)_jlDrawTargetActual; + jlPixelPairT *pixelPair = target + (y * 160) + (x / 2); + if (jlUtilIsOdd(x)) { + pixelPair->l = _jlDrawColor; + } else { + pixelPair->r = _jlDrawColor; + } +} + + +jlSurfaceT jlDrawSurfaceGet(void) { + return _jlDrawTarget; +} + + +void jlDrawSurfaceSet(jlSurfaceT target) { + _jlDrawTarget = target; + _jlDrawTargetActual = (target == NULL) ? (jlSurfaceT)_jlBackingStore->pixels : target; +} + + +bool _jlImgCreate(jlImgT **img) { + jlImgT *t = NULL; + + // Are we loading into a new image? + if (*img == NULL) { + t = (jlImgT *)jlMalloc(sizeof(jlImgT)); + if (t == NULL) { + return false; + } + *img = t; + } + t = (jlImgT *)*img; + t->id[0] = 'I'; + t->id[1] = 'M'; + t->id[2] = 'G'; + t->version = 0; + // Backing store does not exist at startup + if (_jlBackingStore != NULL) { + memcpy(t->palette, _jlBackingStore->palette, sizeof(t->palette)); + memcpy(t->pixels, _jlBackingStore->pixels, sizeof(t->pixels)); + } + *img = t; + return true; +} + + +void jlImgDisplay(jlImgT *img) { + memcpy(_jlBackingStore->palette, img->palette, sizeof(img->palette)); + memcpy(_jlBackingStore->pixels, img->pixels, sizeof(img->pixels)); +} + + +void jlPaletteSet(byte index, byte r, byte g, byte b) { + _jlBackingStore->palette[index].r = r; + _jlBackingStore->palette[index].g = g; + _jlBackingStore->palette[index].b = b; +} + + +void jlPaletteSetFromImg(jlImgT *img) { + memcpy(_jlBackingStore->palette, img->palette, sizeof(img->palette)); +} + + +void jPixBufStart(void) { + // Create backing store + jlImgCreate(_jlBackingStore); + jlPaletteDefault(); + jlDrawSurfaceSet(JOEY_DISPLAY); + jlDrawColorSet(0); + jlDrawClear(); + jlDrawColorSet(15); + jlDisplayPresent(); +} + + +void jPixBufStop(void) { + jlImgFree(_jlBackingStore); +} diff --git a/joeylib/src/jPixBuf.h b/joeylib/src/jPixBuf.h new file mode 100644 index 0000000..14e6383 --- /dev/null +++ b/joeylib/src/jPixBuf.h @@ -0,0 +1,37 @@ +/* + * JoeyLib + * Copyright (C) 2018-2019 Scott Duensing + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. +*/ + + +#ifndef H_JPIXBUF_ +#define H_JPIXBUF_ + + +#include "joey.h" + + +extern jlImgT *_jlBackingStore; + + +void jPixBufStart(void); +void jPixBufStop(void); + + +#endif // H_JPIXBUF_ diff --git a/joeylib/src/jSDL2.c b/joeylib/src/jSDL2.c index 123cce4..c8db036 100644 --- a/joeylib/src/jSDL2.c +++ b/joeylib/src/jSDL2.c @@ -27,6 +27,7 @@ #include "SDL2/SDL_mixer.h" #include "joey.h" +#include "jPixBuf.h" #ifdef DMALLOC @@ -37,7 +38,6 @@ static SDL_Window *_jlWindow = NULL; static SDL_Renderer *_jlRenderer = NULL; -static jlImgT *_jlBackingStore = NULL; // 4 bit representation static SDL_Texture *_jlTexture = NULL; // Video card representation in ARGB static SDL_PixelFormat *_jlPixelFormat = NULL; // Pixel format of _jlTexture static bool _jlIsRunning = true; @@ -49,7 +49,6 @@ static jint16 _jlControllerCount = 0; static juint16 _jlTimerValue = 0; static SDL_TimerID _jlTimerId = 0; - Uint32 _jlUtilTimer(Uint32 interval, void *param); @@ -160,125 +159,6 @@ void jlDisplayPresent(void) { } -void jlDrawBlit8x8(jlSurfaceT source, jint16 sx, jint16 sy, jint16 tx, jint16 ty) { - int o1; - int o2; - int x; - int y; - jlPixelPairT *pixels = (jlPixelPairT *)source; - jlPixelPairT *target = (jlPixelPairT *)_jlDrawTargetActual; - - if (pixels == NULL) { - pixels = (jlPixelPairT *)_jlBackingStore->pixels; - } - - if (jlUtilIsOdd(sx)) sx--; // sx must be even because there are two pixels per byte - o1 = sy * 160 + (int)(sx * 0.5); // This is in pixels... - - if (jlUtilIsOdd(tx)) tx--; // tx must be even because there are two pixels per byte - o2 = ty * 160 + (int)(tx * 0.5); // This is in pixels... - - for (y=0; y<8; y++) { - for (x=0; x<4; x++) { - target[o2++] = pixels[o1++]; - } - o1 += 156; - o2 += 156; - } -} - - -void jlDrawBlit8x8a(jlSurfaceT source, jlStnT *stencil, jint16 sx, jint16 sy, jint16 tx, jint16 ty) { - int so; // Source Pixel Offset - int to; // Target Pixel Offset - int i; - int x; - int y; - int pos; - jlPixelPairT s; // Source Pixel - jlPixelPairT t; // Target Pixel - jlPixelPairT *pixels = (jlPixelPairT *)source; - jlPixelPairT *target = (jlPixelPairT *)_jlDrawTargetActual; - - if (pixels == NULL) { - pixels = (jlPixelPairT *)_jlBackingStore->pixels; - } - - if (jlUtilIsOdd(sx)) sx--; // sx must be even because there are two pixels per byte - so = sy * 160 + (int)(sx * 0.5); // This is in pixels... - - if (jlUtilIsOdd(tx)) tx--; // tx must be even because there are two pixels per byte - to = ty * 160 + (int)(tx * 0.5); // This is in pixels... - - // Color = <-- 40 tiles, 80 bytes, 160 pixels --> - // Stencil = <-- 20 bytes, 160 bits --> - - for (y=0; y<8; y++) { - for (x=0; x<4; x++) { - - t = target[to]; - s = pixels[so++]; - - i = ((sy + y) * 320 + (sx + x * 2)); - pos = 7 - (i % 8); - i /= 8; - - //***FIX*** Another endian order issue. Left & Right are swapped. - if ((stencil->pixels[i] & (1 << pos)) != 0) { - t.r = s.r; - } - - i = ((sy + y) * 320 + (sx + x * 2 + 1)); - pos = 7 - (i % 8); - i /= 8; - - if ((stencil->pixels[i] & (1 << pos)) != 0) { - t.l = s.l; - } - - target[to++] = t; - } - so += 156; - to += 156; - } -} - - -void jlDrawClear(void) { - jlPixelPairT *target = (jlPixelPairT *)_jlDrawTargetActual; - memset(target, _jlDrawColorNibbles, 32000); -} - - -byte jlDrawPixelGet(jint16 x, jint16 y) { - jlPixelPairT *target = (jlPixelPairT *)_jlDrawTargetActual; - int p = x / 2 + y * 160; - return (jlUtilIsOdd(x) ? target[p].l : target[p].r); -} - - -void jlDrawPixelSet(jint16 x, jint16 y) { - jlPixelPairT *target = (jlPixelPairT *)_jlDrawTargetActual; - jlPixelPairT *pixelPair = target + (y * 160) + (x / 2); - if (jlUtilIsOdd(x)) { - pixelPair->l = _jlDrawColor; - } else { - pixelPair->r = _jlDrawColor; - } -} - - -jlSurfaceT jlDrawSurfaceGet(void) { - return _jlDrawTarget; -} - - -void jlDrawSurfaceSet(jlSurfaceT target) { - _jlDrawTarget = target; - _jlDrawTargetActual = (target == NULL) ? (jlSurfaceT)_jlBackingStore->pixels : target; -} - - jint16 jlGameGetAxis(byte which) { SDL_GameControllerAxis axis; short int unscaled; @@ -335,38 +215,6 @@ bool jlGameGetButton(byte which) { } -bool _jlImgCreate(jlImgT **img) { - jlImgT *t = NULL; - - // Are we loading into a new image? - if (*img == NULL) { - t = (jlImgT *)jlMalloc(sizeof(jlImgT)); - if (t == NULL) { - return false; - } - *img = t; - } - t = (jlImgT *)*img; - t->id[0] = 'I'; - t->id[1] = 'M'; - t->id[2] = 'G'; - t->version = 0; - // Backing store does not exist at startup - if (_jlBackingStore != NULL) { - memcpy(t->palette, _jlBackingStore->palette, sizeof(t->palette)); - memcpy(t->pixels, _jlBackingStore->pixels, sizeof(t->pixels)); - } - *img = t; - return true; -} - - -void jlImgDisplay(jlImgT *img) { - memcpy(_jlBackingStore->palette, img->palette, sizeof(img->palette)); - memcpy(_jlBackingStore->pixels, img->pixels, sizeof(img->pixels)); -} - - bool jlKeyPressed(void) { jlUtilIdle(); return (_jlNumKeysDown > 0); @@ -379,18 +227,6 @@ char jlKeyRead(void) { } -void jlPaletteSet(byte index, byte r, byte g, byte b) { - _jlBackingStore->palette[index].r = r; - _jlBackingStore->palette[index].g = g; - _jlBackingStore->palette[index].b = b; -} - - -void jlPaletteSetFromImg(jlImgT *img) { - memcpy(_jlBackingStore->palette, img->palette, sizeof(img->palette)); -} - - void jlSoundFree(jlSoundT *sound) { if (sound != NULL) { if (sound->data != NULL) { @@ -615,28 +451,22 @@ int main(void) { _jlTexture = SDL_CreateTexture(_jlRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 320, 200); _jlPixelFormat = SDL_AllocFormat(SDL_GetWindowPixelFormat(_jlWindow)); - // Create backing store - jlImgCreate(_jlBackingStore); - jlPaletteDefault(); - jlDrawSurfaceSet(JOEY_DISPLAY); - jlDrawColorSet(0); - jlDrawClear(); - jlDrawColorSet(15); - jlDisplayPresent(); - // Start 1/60th second timer _jlTimerId = SDL_AddTimer(1000 / 60, _jlUtilTimer, NULL); + jPixBufStart(); + // Run the main loop. joeyMain(); + jPixBufStop(); + SDL_RemoveTimer(_jlTimerId); for (x=0; x<_jlControllerCount; x++) { SDL_GameControllerClose(_jlControllers[x]); } _jlControllerCount = 0; jlFree(_jlControllers); - jlImgFree(_jlBackingStore); //***TODO*** Sound effects stop? jlSoundModStop(); jlSoundMidiStop();