Skip to content

Commit

Permalink
Most of the game is really just pure C
Browse files Browse the repository at this point in the history
  • Loading branch information
jorio committed Feb 9, 2023
1 parent 6e467c1 commit b09f9d2
Show file tree
Hide file tree
Showing 31 changed files with 157 additions and 156 deletions.
12 changes: 4 additions & 8 deletions src/MTypes.cpp → src/MTypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@


#include "MTypes.h"
#include <algorithm>

using std::min;
using std::max;


void UnionMRect( const MRect* a, const MRect* b, MRect* u )
{
u->top = min( a->top, b->top );
u->left = min( a->left, b->left );
u->bottom = max( a->bottom, b->bottom );
u->right = max( a->right, b->right );
u->top = MinShort( a->top, b->top );
u->left = MinShort( a->left, b->left );
u->bottom = MaxShort( a->bottom, b->bottom );
u->right = MaxShort( a->right, b->right );
}


Expand Down
22 changes: 14 additions & 8 deletions src/MTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,44 @@

#pragma once

#include <cstdint>
#include <stdint.h>
#include <stdbool.h>

typedef signed char MBoolean;

typedef uint32_t MTicks;


struct MRGBColor
typedef struct MRGBColor
{
unsigned short red;
unsigned short green;
unsigned short blue;
};
} MRGBColor;


struct MRect
typedef struct MRect
{
short top;
short left;
short bottom;
short right;
};
short right;
} MRect;


struct MPoint
typedef struct MPoint
{
short v;
short h;
};
} MPoint;


void UnionMRect( const MRect* a, const MRect* b, MRect* u );
void OffsetMRect( MRect* r, int x, int y );
unsigned char MPointInMRect( MPoint p, const MRect* r );


static inline short MinShort(short a, short b) { return a < b ? a : b; }
static inline short MaxShort(short a, short b) { return a < b ? b : a; }
static inline int MinInt(int a, int b) { return a < b ? a : b; }
static inline int MaxInt(int a, int b) { return a < b ? b : a; }
44 changes: 26 additions & 18 deletions src/SDLU.cpp → src/SDLU.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@
#include "SDLU.h"
#include "gameticks.h"
#include "music.h"

#include "main.h" // for Error
#include <deque>

using std::deque;

// for acquiresurface
const int k_acquireMax = 10;
#define k_acquireMax 10
static int s_acquireHead = -1;
static SDL_Surface* s_acquireList[k_acquireMax];

Expand All @@ -38,7 +34,7 @@ static MBoolean s_isForeground = true;
static float s_fadeGamma = 1;

// for checktyping
struct BufferedKey
typedef struct BufferedKey
{
bool isASCII;

Expand All @@ -47,14 +43,18 @@ struct BufferedKey
char ascii;
SDL_Keycode keycode;
} value;
};
} BufferedKey;

#define k_maxBufferedKeys 256

static MBoolean s_interestedInTyping = false;
static std::deque<BufferedKey> s_keyBuffer;
static BufferedKey s_keyBuffer[k_maxBufferedKeys];
static int s_keyBufferSize = 0;

int SDLUi_EventFilter(void*, SDL_Event *event)
int SDLUi_EventFilter(void* junk, SDL_Event *event)
{
(void) junk;

switch (event->type)
{
case SDL_TEXTINPUT:
Expand All @@ -67,7 +67,8 @@ int SDLUi_EventFilter(void*, SDL_Event *event)
BufferedKey key;
key.isASCII = true;
key.value.ascii = *asciiPtr;
s_keyBuffer.push_back(key);
s_keyBuffer[s_keyBufferSize] = key;
s_keyBufferSize = MinInt(k_maxBufferedKeys, s_keyBufferSize + 1);
}
}
break;
Expand All @@ -81,7 +82,8 @@ int SDLUi_EventFilter(void*, SDL_Event *event)
BufferedKey key;
key.isASCII = false;
key.value.keycode = event->key.keysym.sym;
s_keyBuffer.push_back(key);
s_keyBuffer[s_keyBufferSize] = key;
s_keyBufferSize = MinInt(k_maxBufferedKeys, s_keyBufferSize + 1);
}
break;
}
Expand Down Expand Up @@ -385,7 +387,7 @@ MBoolean SDLU_IsForeground()
void SDLU_StartWatchingTyping()
{
s_interestedInTyping = true;
s_keyBuffer.clear();
s_keyBufferSize = 0; // clear keybuffer
}


Expand All @@ -397,10 +399,13 @@ void SDLU_StopWatchingTyping()

MBoolean SDLU_CheckASCIITyping(char* ascii)
{
if (!s_keyBuffer.empty() && s_keyBuffer.front().isASCII)
if (s_keyBufferSize > 0 && s_keyBuffer[0].isASCII)
{
*ascii = s_keyBuffer.front().value.ascii;
s_keyBuffer.pop_front();
*ascii = s_keyBuffer[0].value.ascii;

s_keyBufferSize--;
SDL_memcpy(&s_keyBuffer[0], &s_keyBuffer[1], (s_keyBufferSize) * sizeof(BufferedKey));

return true;
}

Expand All @@ -411,10 +416,13 @@ MBoolean SDLU_CheckASCIITyping(char* ascii)

MBoolean SDLU_CheckSDLTyping(SDL_Keycode* sdlKey)
{
if (!s_keyBuffer.empty() && !s_keyBuffer.front().isASCII)
if (s_keyBufferSize > 0 && !s_keyBuffer[0].isASCII)
{
*sdlKey = s_keyBuffer.front().value.keycode;
s_keyBuffer.pop_front();
*sdlKey = s_keyBuffer[0].value.keycode;

s_keyBufferSize--;
SDL_memcpy(&s_keyBuffer[0], &s_keyBuffer[1], (s_keyBufferSize) * sizeof(BufferedKey));

return true;
}

Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/font.cpp → src/font.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#include "gworld.h"


const int kNumFonts = (picBatsuFont-picFont+1);
#define kNumFonts (picBatsuFont-picFont+1)

static SkittlesFont s_font[kNumFonts] = {};
static SkittlesFont s_font[kNumFonts];


static SkittlesFontPtr LoadFont( SkittlesFontPtr font, int pictID, unsigned char *letterMap )
Expand Down
File renamed without changes.
7 changes: 6 additions & 1 deletion src/graphics.cpp → src/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ SDL_Surface* backdropSurface = NULL;
void DrawSpriteBlobs( int player, int type )
{
MRect firstRect, secondRect, thirdRect;
const int repeat = 0xFF, forever = 0xFE;

#define repeat 0xFF
#define forever 0xFE

static const unsigned char blobAnimation[6][2][25] =
{
Expand Down Expand Up @@ -100,6 +102,9 @@ void DrawSpriteBlobs( int player, int type )
SurfaceDrawSprite( &secondRect, colorB[player], blobAnimation[type][1][anim[player]] );

SDLU_ReleaseSurface( playerSpriteSurface[player] );

#undef repeat
#undef forever
}

void CleanSpriteArea( int player, MRect *myRect )
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions src/gworld.cpp → src/gworld.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ SDL_Surface* LoadPICTAsSurface( int pictID, int depth )
{
const char* filename;
SDL_Surface* surface;
SDL_Rect rect = {};
uint8_t* pixels = nullptr;
SDL_Rect rect = {0};
uint8_t* pixels = NULL;

filename = QuickResourceName( "PICT", pictID, ".jpg" );
if( !FileExists( filename ) )
Expand All @@ -195,7 +195,7 @@ SDL_Surface* LoadPICTAsSurface( int pictID, int depth )
}
if( !FileExists( filename ) )
{
return nullptr;
return NULL;
}

pixels = stbi_load(filename, &rect.w, &rect.h, NULL, 3);
Expand Down
9 changes: 3 additions & 6 deletions src/hiscore.cpp → src/hiscore.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>

#include "main.h"
#include "gworld.h"
Expand All @@ -25,8 +24,6 @@
#include "music.h"
#include "soundfx.h"

using std::min;

Combo defaultBest =
{
/*bestGrid[kGridAcross][kGridDown] = */
Expand Down Expand Up @@ -225,9 +222,9 @@ void ShowHiscore( void )

SDLU_GetPixel( hiScoreSurface, RandomBefore( fullSDLRect.w ), RandomBefore( fullSDLRect.h ), &anyColor );

anyColor.r = min( 255, anyColor.r + 112 );
anyColor.g = min( 255, anyColor.g + 112 );
anyColor.b = min( 255, anyColor.b + 112 );
anyColor.r = MinInt( 255, anyColor.r + 112 );
anyColor.g = MinInt( 255, anyColor.g + 112 );
anyColor.b = MinInt( 255, anyColor.b + 112 );

dPoint.v = widescreen? 100: 20;
dPoint.h = 225;
Expand Down
File renamed without changes.
55 changes: 26 additions & 29 deletions src/level.cpp → src/level.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <stdlib.h>
#include <math.h>
#include <algorithm>

#include "SDLU.h"

Expand Down Expand Up @@ -42,9 +41,6 @@ int difficultyTicks, backdropTicks, backdropFrame;
#define kIncrementPerFrame 2
#define kSplatType 4

using std::min;
using std::max;

const int startSkip = 1;
static MBoolean shouldFullRepaint = false;
static MTicks startMenuTime = 0;
Expand All @@ -63,13 +59,13 @@ enum
kTitleItemQuit,
};

struct TitleItemDef
typedef struct TitleItemDef
{
const char* name;
MRGBColor color1;
MRGBColor color2;
MRect rect;
};
} TitleItemDef;

static const TitleItemDef k_titleItemDefs[kTitleItems] =
{
Expand Down Expand Up @@ -224,21 +220,21 @@ void GameStartMenu( void )
dPoint.v = 215;
for (int i = 0; i < kTitleItems; i++)
{
auto &item = titleItems[i];
item.rect.left = dPoint.h;
item.rect.top = dPoint.v - 6;
item.rect.bottom = dPoint.v + 16 + 6;
auto nameLength = strlen(item.name);
TitleItemDef* item = &titleItems[i];
item->rect.left = dPoint.h;
item->rect.top = dPoint.v - 6;
item->rect.bottom = dPoint.v + 16 + 6;
int nameLength = (int) strlen(item->name);
for (int charNo = 0; charNo < nameLength; charNo++)
{
char c = item.name[charNo];
char c = item->name[charNo];
float p = charNo / (float) (nameLength - 1);
int red = item.color1.red * (1.0f - p) + item.color2.red * p;
int green = item.color1.green * (1.0f - p) + item.color2.green * p;
int blue = item.color1.blue * (1.0f - p) + item.color2.blue * p;
int red = item->color1.red * (1.0f - p) + item->color2.red * p;
int green = item->color1.green * (1.0f - p) + item->color2.green * p;
int blue = item->color1.blue * (1.0f - p) + item->color2.blue * p;
SurfaceBlitCharacter(font, c, &dPoint, red, green, blue, 1);
}
item.rect.right = dPoint.h;
item->rect.right = dPoint.h;
dPoint.h = left;
dPoint.v += 24;
}
Expand Down Expand Up @@ -478,7 +474,7 @@ void GameStartMenu( void )
// update glows
for (int glowUpdate=0; glowUpdate < kTitleItems; ++glowUpdate)
{
const MRect& titleRect = titleItems[glowUpdate].rect;
const MRect* titleRect = &titleItems[glowUpdate].rect;
oldGlow = titleGlow[glowUpdate];

if( selected == glowUpdate )
Expand All @@ -495,13 +491,13 @@ void GameStartMenu( void )
if( titleGlow[glowUpdate] != oldGlow )
{
SurfaceBlitColorOver( gameStartSurface, gameStartDrawSurface,
&titleRect, &titleRect,
titleRect, titleRect,
0, 0, 0, titleGlow[glowUpdate] );

drawRect[kGlow].top = min<short>(drawRect[kGlow].top, titleRect.top);
drawRect[kGlow].left = min<short>(drawRect[kGlow].left, titleRect.left);
drawRect[kGlow].bottom = max<short>(drawRect[kGlow].bottom, titleRect.bottom);
drawRect[kGlow].right = max<short>(drawRect[kGlow].right, titleRect.right);
drawRect[kGlow].top = MinShort(drawRect[kGlow].top, titleRect->top);
drawRect[kGlow].left = MinShort(drawRect[kGlow].left, titleRect->left);
drawRect[kGlow].bottom = MaxShort(drawRect[kGlow].bottom, titleRect->bottom);
drawRect[kGlow].right = MaxShort(drawRect[kGlow].right, titleRect->right);
}
}

Expand Down Expand Up @@ -1274,13 +1270,14 @@ void RegisteredVictory( void )
};

// In widescreen mode, move vertical text positions closer to the center
if (widescreen) {
for (auto& dp : dPoint) {
if (dp.v >= 130) {
dp.v -= 20;
} else {
dp.v += 20;
}
if (widescreen)
{
for (int i = 0; i < arrsize(dPoint); i++)
{
if (dPoint[i].v >= 130)
dPoint[i].v -= 20;
else
dPoint[i].v += 20;
}
}

Expand Down
Loading

0 comments on commit b09f9d2

Please sign in to comment.