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);
SDL_FreeSurface(loadedImage);
}
return optimizedImage;
}
@ -49,10 +48,42 @@ void applySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination, S
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;
}
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
//
@ -239,25 +270,27 @@ int init(int argc, char* args[])
{
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);
font = TTF_OpenFont("assets/charriot.ttf", 10);
@ -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 @@ void cleanup()
SDL_FreeSurface(selectArrow);
SDL_FreeSurface(loadingTxt);
SDL_FreeSurface(screen);
SDL_FreeSurface(window);
printf("Closing SDL\n");
@ -441,7 +480,7 @@ void mainmenu()
while (!select)
{
timeStart(fps);
timeStart(&fps);
applySurface(0, 0, rightHere->bgImage, screen, NULL);
animate(rightHere);
@ -484,9 +523,9 @@ void mainmenu()
case 2:
applySurface(230, 105, selectArrow, screen, NULL);
}
SDL_Flip(screen);
timeDilation();
frameAdvance();
}
if (quit) return;
switch (menucounter)
{
case 0:
@ -558,7 +597,7 @@ void pausemenu()
}
while (paused)
{
timeStart(fps);
timeStart(&fps);
applySurface(60, 45, saveMenu ,screen, NULL);
while(SDL_PollEvent(&event))
{
@ -632,8 +671,7 @@ void pausemenu()
break;
}
}
SDL_Flip(screen);
timeDilation();
frameAdvance();
}
#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);
Uint32 getPixel(SDL_Surface* surface, int x, int y);
void scaleScreen();
// SDL sound
@ -34,6 +38,8 @@ void toggleFullscreen();
void timeDilation();
void frameAdvance();
void cleanup();
void intro();

25
Kaos.c
View file

@ -76,7 +76,7 @@ void runChoice(Kaos* self)
actionbutton = 0;
while (textIsRelevent)
{
timeStart(fps);
timeStart(&fps);
renderBackground();
renderForeground();
@ -118,8 +118,7 @@ void runChoice(Kaos* self)
default: break;
}
}
SDL_Flip(screen);
timeDilation();
frameAdvance();
}
}
@ -244,12 +243,11 @@ void runTeleport(Kaos* self)
}
}
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 @@ void runWait(Kaos* self)
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 @@ void runSpell_Beam(Kaos* self)
for (i = 0; i < 16; i++)
{
timeStart(fps);
timeStart(&fps);
renderBackground();
renderForeground();
switch (facing)
@ -508,8 +505,7 @@ void runSpell_Beam(Kaos* self)
applySurface(offset.x, offset.y, kSelf->aura, screen, &clip);
break;
}
SDL_Flip(screen);
timeDilation();
frameAdvance();
}
}
@ -552,12 +548,11 @@ void runSpell_Flash(Kaos* self)
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();
}
}

View file

@ -126,7 +126,7 @@ void playScene(Scene* self)
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 @@ void playScene(Scene* self)
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++)
{

View file

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

10
Timer.c
View file

@ -11,17 +11,17 @@ Timer* newTimer()
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;
}

View file

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

View file

@ -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:

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_DOWN SDLK_s
@ -11,7 +11,12 @@
#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

View file

@ -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;

6
main.c
View file

@ -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 @@ int main (int argc, char* args[])
while (playing)
{
timeStart(fps);
timeStart(&fps);
interact();
movePlayer(hero, rightHere);
renderBackground();
renderForeground();
renderHUD();
SDL_Flip(screen);
frameAdvance();
kListen(&kaosFlag);
pager();
timeDilation();
}
}