Moved pixel buffer routines into their own file for easier sharing between platforms.

This commit is contained in:
Scott Duensing 2021-03-25 19:39:30 -05:00
parent 5d5cf0fe5d
commit 5207c8f77a
5 changed files with 267 additions and 178 deletions

3
.gitignore vendored
View file

@ -1,5 +1,6 @@
*~ *~
*.user *.user
*.user.*
joeylib/lib/ joeylib/lib/
joeylib/src/SDL2/ joeylib/src/SDL2/
joeylib/src/music joeylib/src/music
@ -13,3 +14,5 @@ assets/JoeyLib Logo.jpg
assets/JoeyLib Logo.png assets/JoeyLib Logo.png
*.stn *.stn
*.img *.img
*.sym
*.ntp

View file

@ -15,6 +15,7 @@ QMAKE_CFLAGS += \
-D_REENTRANT -D_REENTRANT
HEADERS += \ HEADERS += \
src/jPixBuf.h \
src/joey.h \ src/joey.h \
src/stddclmr.h src/stddclmr.h
@ -25,7 +26,9 @@ SOURCES += \
SDL12 { SDL12 {
INCLUDEPATH += $$JOEY/sdks/linux/x64/include INCLUDEPATH += $$JOEY/sdks/linux/x64/include
SOURCES += src/jSDL12.c SOURCES += \
src/jPixBuf.c \
src/jSDL12.c
LIBS += \ LIBS += \
-L$$JOEY/sdks/linux/x64/lib \ -L$$JOEY/sdks/linux/x64/lib \
@ -41,7 +44,9 @@ SDL12 {
SDL2 { SDL2 {
INCLUDEPATH += $$JOEY/sdks/linux/x64/include INCLUDEPATH += $$JOEY/sdks/linux/x64/include
SOURCES += src/jSDL2.c SOURCES += \
src/jPixBuf.c \
src/jSDL2.c
LIBS += \ LIBS += \
-L$$JOEY/sdks/linux/x64/lib \ -L$$JOEY/sdks/linux/x64/lib \
@ -57,7 +62,7 @@ SDL2 {
} }
OTHER_FILES += \ OTHER_FILES += \
src/jBlankcd ..c \ src/jBlank.c \
src/jAmiga.c \ src/jAmiga.c \
src/jIIgs.c \ src/jIIgs.c \
src/jIIgs.asm \ src/jIIgs.asm \

214
joeylib/src/jPixBuf.c Normal file
View file

@ -0,0 +1,214 @@
/*
* JoeyLib
* Copyright (C) 2018-2019 Scott Duensing <scott@kangaroopunch.com>
*
* 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 <string.h>
#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);
}

37
joeylib/src/jPixBuf.h Normal file
View file

@ -0,0 +1,37 @@
/*
* JoeyLib
* Copyright (C) 2018-2019 Scott Duensing <scott@kangaroopunch.com>
*
* 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_

View file

@ -27,6 +27,7 @@
#include "SDL2/SDL_mixer.h" #include "SDL2/SDL_mixer.h"
#include "joey.h" #include "joey.h"
#include "jPixBuf.h"
#ifdef DMALLOC #ifdef DMALLOC
@ -37,7 +38,6 @@
static SDL_Window *_jlWindow = NULL; static SDL_Window *_jlWindow = NULL;
static SDL_Renderer *_jlRenderer = 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_Texture *_jlTexture = NULL; // Video card representation in ARGB
static SDL_PixelFormat *_jlPixelFormat = NULL; // Pixel format of _jlTexture static SDL_PixelFormat *_jlPixelFormat = NULL; // Pixel format of _jlTexture
static bool _jlIsRunning = true; static bool _jlIsRunning = true;
@ -49,7 +49,6 @@ static jint16 _jlControllerCount = 0;
static juint16 _jlTimerValue = 0; static juint16 _jlTimerValue = 0;
static SDL_TimerID _jlTimerId = 0; static SDL_TimerID _jlTimerId = 0;
Uint32 _jlUtilTimer(Uint32 interval, void *param); 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) { jint16 jlGameGetAxis(byte which) {
SDL_GameControllerAxis axis; SDL_GameControllerAxis axis;
short int unscaled; 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) { bool jlKeyPressed(void) {
jlUtilIdle(); jlUtilIdle();
return (_jlNumKeysDown > 0); 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) { void jlSoundFree(jlSoundT *sound) {
if (sound != NULL) { if (sound != NULL) {
if (sound->data != NULL) { if (sound->data != NULL) {
@ -615,28 +451,22 @@ int main(void) {
_jlTexture = SDL_CreateTexture(_jlRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 320, 200); _jlTexture = SDL_CreateTexture(_jlRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 320, 200);
_jlPixelFormat = SDL_AllocFormat(SDL_GetWindowPixelFormat(_jlWindow)); _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 // Start 1/60th second timer
_jlTimerId = SDL_AddTimer(1000 / 60, _jlUtilTimer, NULL); _jlTimerId = SDL_AddTimer(1000 / 60, _jlUtilTimer, NULL);
jPixBufStart();
// Run the main loop. // Run the main loop.
joeyMain(); joeyMain();
jPixBufStop();
SDL_RemoveTimer(_jlTimerId); SDL_RemoveTimer(_jlTimerId);
for (x=0; x<_jlControllerCount; x++) { for (x=0; x<_jlControllerCount; x++) {
SDL_GameControllerClose(_jlControllers[x]); SDL_GameControllerClose(_jlControllers[x]);
} }
_jlControllerCount = 0; _jlControllerCount = 0;
jlFree(_jlControllers); jlFree(_jlControllers);
jlImgFree(_jlBackingStore);
//***TODO*** Sound effects stop? //***TODO*** Sound effects stop?
jlSoundModStop(); jlSoundModStop();
jlSoundMidiStop(); jlSoundMidiStop();