fixed timers for proper constant framerate, and VIDEO SCALING!!!

This commit is contained in:
Iris Lightshard 2019-01-09 14:26:38 -08:00
parent 19968d19bf
commit 1f976f1ef7
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398
12 changed files with 95 additions and 51 deletions

View file

@ -40,7 +40,6 @@ SDL_Surface* loadImage(char* filename)
optimizedImage = SDL_DisplayFormat(loadedImage); optimizedImage = SDL_DisplayFormat(loadedImage);
SDL_FreeSurface(loadedImage); SDL_FreeSurface(loadedImage);
} }
return optimizedImage; return optimizedImage;
} }
@ -49,10 +48,42 @@ void applySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination, S
SDL_Rect offset; SDL_Rect offset;
offset.x = x; offset.x = x;
offset.y = y; offset.y = y;
SDL_BlitSurface(source, clip, destination, &offset); 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;
}
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;
}
}
// //
// sound // sound
// //
@ -239,25 +270,27 @@ int init(int argc, char* args[])
{ {
if (strcmp(args[1], "-w")) if (strcmp(args[1], "-w"))
{ {
screen = SDL_SetVideoMode( 320, 180, 32, SDL_FULLSCREEN|SDL_HWSURFACE); window = SDL_SetVideoMode( SCREEN_WIDTH*SCALE_FACTOR, SCREEN_HEIGHT*SCALE_FACTOR, 32, SDL_FULLSCREEN|SDL_HWSURFACE);
SDL_ShowCursor(0);
fullscreen = 1; 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 else
{ {
screen = SDL_SetVideoMode( 320, 180, 32, SDL_FULLSCREEN|SDL_HWSURFACE); window = SDL_SetVideoMode( SCREEN_WIDTH*SCALE_FACTOR, SCREEN_HEIGHT*SCALE_FACTOR, 32, SDL_FULLSCREEN|SDL_HWSURFACE);
SDL_ShowCursor(0);
fullscreen = 1; 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); SDL_WM_SetCaption("Kaos Mage's Infinite Sidequest", NULL);
printf("Window created\nInitializing fonts\n"); printf("Window created\nInitializing fonts\n");
if (TTF_Init() == -1) return 0; if (TTF_Init() == -1) return 0;
printf("Fonts initialized\nInitializing data\n"); printf("Fonts initialized\nInitializing data\n");
menuBG = newRoom("assets/img/backgrounds/mainmenu.png", 1); menuBG = newRoom("assets/img/backgrounds/mainmenu.png", 1);
font = TTF_OpenFont("assets/charriot.ttf", 10); font = TTF_OpenFont("assets/charriot.ttf", 10);
@ -325,26 +358,31 @@ void toggleFullscreen()
{ {
if (!fullscreen) if (!fullscreen)
{ {
SDL_SetVideoMode(320, 180, 32, SDL_FULLSCREEN|SDL_HWSURFACE); SDL_SetVideoMode(SCREEN_WIDTH*SCALE_FACTOR, SCREEN_HEIGHT*SCALE_FACTOR, 32, SDL_FULLSCREEN|SDL_HWSURFACE);
SDL_ShowCursor(0);
fullscreen = 1; fullscreen = 1;
} }
else else
{ {
SDL_SetVideoMode(320, 180, 32, SDL_HWSURFACE|SDL_RESIZABLE); SDL_SetVideoMode(SCREEN_WIDTH*SCALE_FACTOR, SCREEN_HEIGHT*SCALE_FACTOR, 32, SDL_HWSURFACE|SDL_RESIZABLE);
SDL_ShowCursor(1);
fullscreen = 0; fullscreen = 0;
} }
} }
void timeDilation() void timeDilation()
{ {
int i = getTicks(fps); int i = getTicks(&fps);
if (i < 1000 / 30) if (i < 1000 / 30)
SDL_Delay(1000/30 - i); SDL_Delay(1000/30 - i);
} }
void frameAdvance()
{
scaleScreen();
SDL_Flip(window);
timeDilation();
}
void cleanup() void cleanup()
{ {
if(fullscreen) if(fullscreen)
@ -377,6 +415,7 @@ void cleanup()
SDL_FreeSurface(selectArrow); SDL_FreeSurface(selectArrow);
SDL_FreeSurface(loadingTxt); SDL_FreeSurface(loadingTxt);
SDL_FreeSurface(screen); SDL_FreeSurface(screen);
SDL_FreeSurface(window);
printf("Closing SDL\n"); printf("Closing SDL\n");
@ -441,7 +480,7 @@ void mainmenu()
while (!select) while (!select)
{ {
timeStart(fps); timeStart(&fps);
applySurface(0, 0, rightHere->bgImage, screen, NULL); applySurface(0, 0, rightHere->bgImage, screen, NULL);
animate(rightHere); animate(rightHere);
@ -484,9 +523,9 @@ void mainmenu()
case 2: case 2:
applySurface(230, 105, selectArrow, screen, NULL); applySurface(230, 105, selectArrow, screen, NULL);
} }
SDL_Flip(screen); frameAdvance();
timeDilation();
} }
if (quit) return;
switch (menucounter) switch (menucounter)
{ {
case 0: case 0:
@ -558,7 +597,7 @@ void pausemenu()
} }
while (paused) while (paused)
{ {
timeStart(fps); timeStart(&fps);
applySurface(60, 45, saveMenu ,screen, NULL); applySurface(60, 45, saveMenu ,screen, NULL);
while(SDL_PollEvent(&event)) while(SDL_PollEvent(&event))
{ {
@ -632,8 +671,7 @@ void pausemenu()
break; break;
} }
} }
SDL_Flip(screen); frameAdvance();
timeDilation();
} }
#ifdef SOUND_ON #ifdef SOUND_ON

View file

@ -4,6 +4,10 @@ SDL_Surface* loadImage(char* filename);
void applySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip); 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 // SDL sound
@ -34,6 +38,8 @@ void toggleFullscreen();
void timeDilation(); void timeDilation();
void frameAdvance();
void cleanup(); void cleanup();
void intro(); void intro();

25
Kaos.c
View file

@ -76,7 +76,7 @@ void runChoice(Kaos* self)
actionbutton = 0; actionbutton = 0;
while (textIsRelevent) while (textIsRelevent)
{ {
timeStart(fps); timeStart(&fps);
renderBackground(); renderBackground();
renderForeground(); renderForeground();
@ -118,8 +118,7 @@ void runChoice(Kaos* self)
default: break; default: break;
} }
} }
SDL_Flip(screen); frameAdvance();
timeDilation();
} }
} }
@ -244,12 +243,11 @@ void runTeleport(Kaos* self)
} }
} }
clip.x = (i%4)*32; clip.x = (i%4)*32;
timeStart(fps); timeStart(&fps);
renderBackground(); renderBackground();
renderForeground(); renderForeground();
applySurface(kSelf->x-16, kSelf->y-16, kSelf->aura, screen, &clip); applySurface(kSelf->x-16, kSelf->y-16, kSelf->aura, screen, &clip);
SDL_Flip(screen); frameAdvance();
timeDilation();
} }
} }
@ -407,13 +405,12 @@ void runWait(Kaos* self)
for (i = 0; i < kSelf->frames; i++) for (i = 0; i < kSelf->frames; i++)
{ {
timeStart(fps); timeStart(&fps);
if (captive) if (captive)
interact(); interact();
renderBackground(); renderBackground();
renderForeground(); renderForeground();
SDL_Flip(screen); frameAdvance();
timeDilation();
} }
captive = 0; captive = 0;
} }
@ -489,7 +486,7 @@ void runSpell_Beam(Kaos* self)
for (i = 0; i < 16; i++) for (i = 0; i < 16; i++)
{ {
timeStart(fps); timeStart(&fps);
renderBackground(); renderBackground();
renderForeground(); renderForeground();
switch (facing) switch (facing)
@ -508,8 +505,7 @@ void runSpell_Beam(Kaos* self)
applySurface(offset.x, offset.y, kSelf->aura, screen, &clip); applySurface(offset.x, offset.y, kSelf->aura, screen, &clip);
break; break;
} }
SDL_Flip(screen); frameAdvance();
timeDilation();
} }
} }
@ -552,12 +548,11 @@ void runSpell_Flash(Kaos* self)
for (i = 0; i < 8; i++) for (i = 0; i < 8; i++)
{ {
clip.x = 41*i; clip.x = 41*i;
timeStart(fps); timeStart(&fps);
renderBackground(); renderBackground();
renderForeground(); renderForeground();
applySurface(offset.x, offset.y, kSelf->aura, screen, &clip); applySurface(offset.x, offset.y, kSelf->aura, screen, &clip);
SDL_Flip(screen); frameAdvance();
timeDilation();
} }
} }

View file

@ -126,7 +126,7 @@ void playScene(Scene* self)
for (i = 0; i < self->time; i++) for (i = 0; i < self->time; i++)
{ {
if (quit) return; if (quit) return;
timeStart(fps); timeStart(&fps);
sceneInput(self, &i); sceneInput(self, &i);
for (j = 0; j < self->nSprites; j++) for (j = 0; j < self->nSprites; j++)
{ {
@ -146,8 +146,7 @@ void playScene(Scene* self)
SDL_SetAlpha(fadeOut, SDL_SRCALPHA|SDL_RLEACCEL, alphamod); SDL_SetAlpha(fadeOut, SDL_SRCALPHA|SDL_RLEACCEL, alphamod);
applySurface(0,0, fadeOut, screen, NULL); applySurface(0,0, fadeOut, screen, NULL);
} }
SDL_Flip(screen); frameAdvance();
timeDilation();
} }
for (j = 0; j < self->nSprites; j++) for (j = 0; j < self->nSprites; j++)
{ {

View file

@ -139,7 +139,7 @@ void displayTextBox(TextBox* self)
actionbutton = 0; actionbutton = 0;
while (textIsRelevent) while (textIsRelevent)
{ {
timeStart(fps); timeStart(&fps);
renderBackground(); renderBackground();
renderForeground(); renderForeground();

10
Timer.c
View file

@ -11,17 +11,17 @@ Timer* newTimer()
return self; 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) if (self->startTicks)
return SDL_GetTicks() - self.startTicks; return SDL_GetTicks() - self->startTicks;
else return 0; else return 0;
} }

View file

@ -4,5 +4,5 @@ typedef struct timer
} Timer; } Timer;
void timeStart(Timer self); void timeStart(Timer* self);
int getTicks(Timer self); int getTicks(Timer* self);

View file

@ -25,7 +25,7 @@ void bufferData(enum dataChunks chunk)
{ {
hasMusic = 0; hasMusic = 0;
applySurface(0,0, loadingTxt, screen, NULL); applySurface(0,0, loadingTxt, screen, NULL);
SDL_Flip(screen); frameAdvance();
printf("Loading map chunk\n"); printf("Loading map chunk\n");
switch (chunk){ switch (chunk){
case LEVEL1: case LEVEL1:

BIN
assets/img/rawscreen.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 114 KiB

View file

@ -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_UP SDLK_w
#define DPAD_DOWN SDLK_s #define DPAD_DOWN SDLK_s
@ -11,7 +11,12 @@
#define FS_BUTTON SDLK_f #define FS_BUTTON SDLK_f
#define PAUSE_BUTTON SDLK_q #define PAUSE_BUTTON SDLK_q
// internal game resolution is 320x180 widescreen
#define SCREEN_WIDTH 320 #define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 180 #define SCREEN_HEIGHT 180
// video scaling factor should be a positive integer
#define SCALE_FACTOR 2
//#define SOUND_ON //#define SOUND_ON

View file

@ -9,6 +9,7 @@ extern int hasMusic;
extern SDL_Event event; extern SDL_Event event;
extern SDL_Surface* screen; extern SDL_Surface* screen;
extern SDL_Surface* window;
extern Timer fps; extern Timer fps;
extern Room* rightHere; extern Room* rightHere;
extern Player* hero; extern Player* hero;

6
main.c
View file

@ -32,6 +32,7 @@ Timer fps = { 0 };
SDL_Event event; SDL_Event event;
SDL_Surface* screen = NULL; SDL_Surface* screen = NULL;
SDL_Surface* window = NULL;
Room* rightHere = NULL; Room* rightHere = NULL;
HyperKaos** spellBook = NULL; HyperKaos** spellBook = NULL;
@ -85,16 +86,15 @@ int main (int argc, char* args[])
while (playing) while (playing)
{ {
timeStart(fps); timeStart(&fps);
interact(); interact();
movePlayer(hero, rightHere); movePlayer(hero, rightHere);
renderBackground(); renderBackground();
renderForeground(); renderForeground();
renderHUD(); renderHUD();
SDL_Flip(screen); frameAdvance();
kListen(&kaosFlag); kListen(&kaosFlag);
pager(); pager();
timeDilation();
} }
} }