diff --git a/Engine.c b/Engine.c index 201fba7..abd0250 100644 --- a/Engine.c +++ b/Engine.c @@ -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 diff --git a/Engine.h b/Engine.h index f6eaf82..5c035bb 100644 --- a/Engine.h +++ b/Engine.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 @@ -34,6 +38,8 @@ void toggleFullscreen(); void timeDilation(); +void frameAdvance(); + void cleanup(); void intro(); diff --git a/Kaos.c b/Kaos.c index e18a0de..1c1876f 100644 --- a/Kaos.c +++ b/Kaos.c @@ -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(); } } diff --git a/Scene.c b/Scene.c index a2f459b..1bdecf1 100644 --- a/Scene.c +++ b/Scene.c @@ -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++) { diff --git a/TextBox.c b/TextBox.c index 1a48115..eb8ebd4 100644 --- a/TextBox.c +++ b/TextBox.c @@ -139,7 +139,7 @@ void displayTextBox(TextBox* self) actionbutton = 0; while (textIsRelevent) { - timeStart(fps); + timeStart(&fps); renderBackground(); renderForeground(); diff --git a/Timer.c b/Timer.c index f588bf7..a857006 100644 --- a/Timer.c +++ b/Timer.c @@ -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; } diff --git a/Timer.h b/Timer.h index 541ea09..e181f80 100644 --- a/Timer.h +++ b/Timer.h @@ -4,5 +4,5 @@ typedef struct timer } Timer; -void timeStart(Timer self); -int getTicks(Timer self); \ No newline at end of file +void timeStart(Timer* self); +int getTicks(Timer* self); \ No newline at end of file diff --git a/WorldData.c b/WorldData.c index f7b7558..553be7c 100644 --- a/WorldData.c +++ b/WorldData.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: diff --git a/assets/img/rawscreen.png b/assets/img/rawscreen.png new file mode 100644 index 0000000..60b5089 Binary files /dev/null and b/assets/img/rawscreen.png differ diff --git a/config.h b/config.h index 999cd0b..5cfb12b 100644 --- a/config.h +++ b/config.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 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 diff --git a/extern.h b/extern.h index 6f3f0bd..a72e8ac 100644 --- a/extern.h +++ b/extern.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; diff --git a/main.c b/main.c index 86ade44..83d9eca 100644 --- a/main.c +++ b/main.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 @@ 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(); } }