all repos — hyperkaos @ 1f976f1ef7214df3dc68466edbfa84d68ba57c01

lightweight modular puzzle/adventure game engine in C with SDL 1.2

fixed timers for proper constant framerate, and VIDEO SCALING!!!
Derek Stevens nilix@nilfm.cc
PGP Signature
-----BEGIN PGP SIGNATURE-----

iQIzBAABCAAdFiEEkFh6dA+k/6CXFXU4O3+8IhROY5gFAlw2dR4ACgkQO3+8IhRO
Y5iN+w//X2eFogHvwQWfCvgE0O8XUiQz7Os1XZoP9oLFu3kizm8u4jGMx0H6+rLX
6Fii+ERIMCRSHjTdeU50gVLIqk1T0IPCsHUrBMA8uAkDKNPakriFORvBqOKX/IcN
3cYbgIbjAdlLJRU+3/nbJ3JfwmSO/Az6l/3IYI1t7CGkxR6Mbh7Lo065mo2hhAl0
+sag9HRwE04fHDdjbjgpSaUvR5TzvpGMrtPWmfPAEs4L4KLBADZB93Y0lrqzOZuu
7Fq/lFpIBN89zWq7VL0Rf/x1ngOSNLJMQVDSLnllmiEVWjwGO//8rIupMt/T5+ZN
oy5OriM24Qre+My/c1wP4eZs3iZwmdHf6qdIJoSYlQI9UuYhyD3VZvEUDUqHh9Cy
Qg/mNwC80OSSF36UVKG9PiGzSMLMtcFz4RtKZatyzGDYAJ6yhBnnD2Vb8dZi7UM3
T65iKdd8HXy/BqMEGNq+os21d6xwghGK35k0VulzEep4KMCLIpQisv1MfOqt8MH8
vVnRfIiNj+xS4tBTpix7z1sLMFUi0K/Xpz4nURXyXMklGKxwCEvFa+o4GRjxRnCB
4j5MUgP2YIPId34X4WtsduhYzs04Dcl3kS34168uc6Dkf3Spmw/hxF08VNkZe+VT
ZcvZTGy+ShDP7ZgltS5mtLIIjAY/AHGQlfMhCgSO33CJwA3sOoo=
=WN9a
-----END PGP SIGNATURE-----
commit

1f976f1ef7214df3dc68466edbfa84d68ba57c01

parent

19968d19bf28307f0566784135ec6844785b27e1

12 files changed, 95 insertions(+), 51 deletions(-)

jump to
M Engine.cEngine.c

@@ -40,7 +40,6 @@ {

optimizedImage = SDL_DisplayFormat(loadedImage); SDL_FreeSurface(loadedImage); } - return optimizedImage; }

@@ -49,8 +48,40 @@ {

SDL_Rect offset; offset.x = x; offset.y = y; + SDL_BlitSurface(source, clip, destination, &offset); +} + +Uint32 getPixel(SDL_Surface* surface, int x, int y) +{ + Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * surface->format->BytesPerPixel; + return *(Uint32 *)p; +} - SDL_BlitSurface(source, clip, destination, &offset); +void scaleScreen() +{ + Sint32 x, y; + static SDL_Rect superPixel = {0,0, SCALE_FACTOR,SCALE_FACTOR}; + + switch (SCALE_FACTOR) + { + case 1: + applySurface(0,0,screen,window,NULL); + break; + + default: + superPixel.y = 0; + for (y = 0; y < SCREEN_HEIGHT; y++) + { + superPixel.x = 0; + for (x = 0; x < SCREEN_WIDTH; x++) + { + SDL_FillRect(window, &superPixel, getPixel(screen, x, y)); + superPixel.x += SCALE_FACTOR; + } + superPixel.y += SCALE_FACTOR; + } + break; + } } //

@@ -239,24 +270,26 @@ if (argc >= 2)

{ if (strcmp(args[1], "-w")) { - screen = SDL_SetVideoMode( 320, 180, 32, SDL_FULLSCREEN|SDL_HWSURFACE); - SDL_ShowCursor(0); + window = SDL_SetVideoMode( SCREEN_WIDTH*SCALE_FACTOR, SCREEN_HEIGHT*SCALE_FACTOR, 32, SDL_FULLSCREEN|SDL_HWSURFACE); fullscreen = 1; } - else screen = SDL_SetVideoMode( 320, 180, 32, SDL_HWSURFACE|SDL_RESIZABLE); + else + { + window = SDL_SetVideoMode( SCREEN_WIDTH*SCALE_FACTOR, SCREEN_HEIGHT*SCALE_FACTOR, 32, SDL_HWSURFACE|SDL_RESIZABLE); + } } else { - screen = SDL_SetVideoMode( 320, 180, 32, SDL_FULLSCREEN|SDL_HWSURFACE); - SDL_ShowCursor(0); + window = SDL_SetVideoMode( SCREEN_WIDTH*SCALE_FACTOR, SCREEN_HEIGHT*SCALE_FACTOR, 32, SDL_FULLSCREEN|SDL_HWSURFACE); fullscreen = 1; } - if (screen == NULL) return 0; + screen = loadImage("assets/img/rawscreen.png"); + if (window == NULL || screen == NULL) return 0; + SDL_ShowCursor(0); SDL_WM_SetCaption("Kaos Mage's Infinite Sidequest", NULL); printf("Window created\nInitializing fonts\n"); if (TTF_Init() == -1) return 0; printf("Fonts initialized\nInitializing data\n"); - menuBG = newRoom("assets/img/backgrounds/mainmenu.png", 1);

@@ -325,26 +358,31 @@ void toggleFullscreen()

{ if (!fullscreen) { - SDL_SetVideoMode(320, 180, 32, SDL_FULLSCREEN|SDL_HWSURFACE); - SDL_ShowCursor(0); + SDL_SetVideoMode(SCREEN_WIDTH*SCALE_FACTOR, SCREEN_HEIGHT*SCALE_FACTOR, 32, SDL_FULLSCREEN|SDL_HWSURFACE); fullscreen = 1; } else { - SDL_SetVideoMode(320, 180, 32, SDL_HWSURFACE|SDL_RESIZABLE); - SDL_ShowCursor(1); + SDL_SetVideoMode(SCREEN_WIDTH*SCALE_FACTOR, SCREEN_HEIGHT*SCALE_FACTOR, 32, SDL_HWSURFACE|SDL_RESIZABLE); fullscreen = 0; } } void timeDilation() { - int i = getTicks(fps); + int i = getTicks(&fps); if (i < 1000 / 30) SDL_Delay(1000/30 - i); } +void frameAdvance() +{ + scaleScreen(); + SDL_Flip(window); + timeDilation(); +} + void cleanup() { if(fullscreen)

@@ -377,6 +415,7 @@ SDL_FreeSurface(saveMenu);

SDL_FreeSurface(selectArrow); SDL_FreeSurface(loadingTxt); SDL_FreeSurface(screen); + SDL_FreeSurface(window); printf("Closing SDL\n");

@@ -441,7 +480,7 @@ #endif

while (!select) { - timeStart(fps); + timeStart(&fps); applySurface(0, 0, rightHere->bgImage, screen, NULL); animate(rightHere);

@@ -484,9 +523,9 @@ break;

case 2: applySurface(230, 105, selectArrow, screen, NULL); } - SDL_Flip(screen); - timeDilation(); + frameAdvance(); } + if (quit) return; switch (menucounter) { case 0:

@@ -558,7 +597,7 @@ }

} while (paused) { - timeStart(fps); + timeStart(&fps); applySurface(60, 45, saveMenu ,screen, NULL); while(SDL_PollEvent(&event)) {

@@ -632,8 +671,7 @@ playing = 0;

break; } } - SDL_Flip(screen); - timeDilation(); + frameAdvance(); } #ifdef SOUND_ON
M Engine.hEngine.h

@@ -4,6 +4,10 @@ SDL_Surface* loadImage(char* filename);

void applySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip); +Uint32 getPixel(SDL_Surface* surface, int x, int y); + +void scaleScreen(); + // SDL sound

@@ -33,6 +37,8 @@

void toggleFullscreen(); void timeDilation(); + +void frameAdvance(); void cleanup();
M Kaos.cKaos.c

@@ -76,7 +76,7 @@ int textIsRelevent = 1;

actionbutton = 0; while (textIsRelevent) { - timeStart(fps); + timeStart(&fps); renderBackground(); renderForeground();

@@ -118,8 +118,7 @@ }

default: break; } } - SDL_Flip(screen); - timeDilation(); + frameAdvance(); } }

@@ -244,12 +243,11 @@ kSelf->target->point.y = kSelf->y;

} } clip.x = (i%4)*32; - timeStart(fps); + timeStart(&fps); renderBackground(); renderForeground(); applySurface(kSelf->x-16, kSelf->y-16, kSelf->aura, screen, &clip); - SDL_Flip(screen); - timeDilation(); + frameAdvance(); } }

@@ -407,13 +405,12 @@ captive = 1;

for (i = 0; i < kSelf->frames; i++) { - timeStart(fps); + timeStart(&fps); if (captive) interact(); renderBackground(); renderForeground(); - SDL_Flip(screen); - timeDilation(); + frameAdvance(); } captive = 0; }

@@ -489,7 +486,7 @@ }

for (i = 0; i < 16; i++) { - timeStart(fps); + timeStart(&fps); renderBackground(); renderForeground(); switch (facing)

@@ -508,8 +505,7 @@ clip.y = (i%4)*32;

applySurface(offset.x, offset.y, kSelf->aura, screen, &clip); break; } - SDL_Flip(screen); - timeDilation(); + frameAdvance(); } }

@@ -552,12 +548,11 @@ clip.h = 72;

for (i = 0; i < 8; i++) { clip.x = 41*i; - timeStart(fps); + timeStart(&fps); renderBackground(); renderForeground(); applySurface(offset.x, offset.y, kSelf->aura, screen, &clip); - SDL_Flip(screen); - timeDilation(); + frameAdvance(); } }
M Scene.cScene.c

@@ -126,7 +126,7 @@

for (i = 0; i < self->time; i++) { if (quit) return; - timeStart(fps); + timeStart(&fps); sceneInput(self, &i); for (j = 0; j < self->nSprites; j++) {

@@ -146,8 +146,7 @@ alphamod = 255 - (int)(255*((float)(self->time - i)/self->fade.out));

SDL_SetAlpha(fadeOut, SDL_SRCALPHA|SDL_RLEACCEL, alphamod); applySurface(0,0, fadeOut, screen, NULL); } - SDL_Flip(screen); - timeDilation(); + frameAdvance(); } for (j = 0; j < self->nSprites; j++) {
M TextBox.cTextBox.c

@@ -139,7 +139,7 @@ SDL_Rect textScroller = {0, 0, 0, 14};

actionbutton = 0; while (textIsRelevent) { - timeStart(fps); + timeStart(&fps); renderBackground(); renderForeground();
M Timer.cTimer.c

@@ -11,17 +11,17 @@ self->startTicks = 0;

return self; } -void timeStart(Timer self) +void timeStart(Timer* self) { - self.startTicks = SDL_GetTicks(); + self->startTicks = SDL_GetTicks(); } -int getTicks(Timer self) +int getTicks(Timer* self) { - if (self.startTicks) - return SDL_GetTicks() - self.startTicks; + if (self->startTicks) + return SDL_GetTicks() - self->startTicks; else return 0; }
M Timer.hTimer.h

@@ -4,5 +4,5 @@ int startTicks;

} Timer; -void timeStart(Timer self); -int getTicks(Timer self);+void timeStart(Timer* self); +int getTicks(Timer* self);
M WorldData.cWorldData.c

@@ -25,7 +25,7 @@ void bufferData(enum dataChunks chunk)

{ hasMusic = 0; applySurface(0,0, loadingTxt, screen, NULL); - SDL_Flip(screen); + frameAdvance(); printf("Loading map chunk\n"); switch (chunk){ case LEVEL1:
M config.hconfig.h

@@ -1,4 +1,4 @@

-/* see http://sdlk.beuc.net/sdl.wiki/SDLKey for keysyms */ +/* see https://sdl.beuc.net/sdl.wiki/SDLKey for keysyms */ #define DPAD_UP SDLK_w #define DPAD_DOWN SDLK_s

@@ -11,7 +11,12 @@ #define R_BUTTON SDLK_p

#define FS_BUTTON SDLK_f #define PAUSE_BUTTON SDLK_q + +// internal game resolution is 320x180 widescreen #define SCREEN_WIDTH 320 #define SCREEN_HEIGHT 180 + +// video scaling factor should be a positive integer +#define SCALE_FACTOR 2 //#define SOUND_ON
M extern.hextern.h

@@ -9,6 +9,7 @@ extern int hasMusic;

extern SDL_Event event; extern SDL_Surface* screen; +extern SDL_Surface* window; extern Timer fps; extern Room* rightHere; extern Player* hero;
M main.cmain.c

@@ -32,6 +32,7 @@ Timer fps = { 0 };

SDL_Event event; SDL_Surface* screen = NULL; +SDL_Surface* window = NULL; Room* rightHere = NULL; HyperKaos** spellBook = NULL;

@@ -85,16 +86,15 @@ mainmenu();

while (playing) { - timeStart(fps); + timeStart(&fps); interact(); movePlayer(hero, rightHere); renderBackground(); renderForeground(); renderHUD(); - SDL_Flip(screen); + frameAdvance(); kListen(&kaosFlag); pager(); - timeDilation(); } }