katbug/Engine.c

441 lines
8.8 KiB
C
Raw Permalink Normal View History

2019-02-04 21:24:24 +00:00
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "config.h"
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include "Engine.h"
#include "Timer.h"
#include "Catbug.h"
#include "Pickups.h"
#include "extern.h"
SDL_Surface* loadImage(char* filename)
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load(filename);
if (loadedImage != NULL)
{
optimizedImage = SDL_DisplayFormat(loadedImage);
SDL_FreeSurface(loadedImage);
}
return optimizedImage;
}
void applySurface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip)
{
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;
}
}
void updateScore()
{
SDL_Color white = {255,255,255};
char sPoints[10];
sprintf(sPoints, "%d", points);
score = TTF_RenderText_Solid(font,sPoints,white);
applySurface(10, 20, score, screen, NULL);
}
void drawHP()
{
int i;
for (i = 0; i < player->HP; i++)
{
applySurface(10+(6*i), 10, hpwedge, screen, NULL);
}
}
void checkHP()
{
if (player->HP <= 0)
{
scoreSummary();
quit = 1;
}
}
void scoreSummary()
{
SDL_Surface* yougot = NULL;
SDL_Surface* hiscore = NULL;
SDL_Color white = {255, 255, 255};
char sPoints[10];
sprintf(sPoints,"%d", points);
yougot = TTF_RenderText_Solid(font2, "SCORE:" ,white);
hiscore = TTF_RenderText_Solid(font2, sPoints, white);
int here = 1;
while (here)
{
timeStart(&fps);
renderBG();
applySurface(120, 80, yougot, screen, NULL);
applySurface(120, 105, hiscore, screen, NULL);
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case FS_BUTTON:
toggleFullscreen();
break;
case PAUSE_BUTTON:
here = 0;
quit = 0;
break;
case QUIT_BUTTON:
here = 0;
playing = 0;
default:
break;
}
break;
case SDL_QUIT:
here = 0;
playing = 0;
break;
default:
break;
}
}
frameAdvance();
}
SDL_FreeSurface(yougot);
SDL_FreeSurface(hiscore);
}
void renderBG()
{
static int x = 0;
applySurface(x,0,bg,screen,NULL);
applySurface(x+1472,0,bg,screen,NULL);
x -= 8;
if (x == -1472) x = 0;
}
void interact()
{
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case DPAD_UP:
player->vY = -PLAYER_SPD;
break;
case DPAD_LEFT:
player->vX = -PLAYER_SPD;
break;
case DPAD_DOWN:
player->vY = PLAYER_SPD;
break;
case DPAD_RIGHT:
player->vX = PLAYER_SPD;
break;
case FS_BUTTON:
toggleFullscreen();
break;
case PAUSE_BUTTON:
pauseGame();
break;
case QUIT_BUTTON:
quit = 1;
playing = 0;
break;
default:
break;
}
break;
case SDL_KEYUP:
switch (event.key.keysym.sym)
{
case DPAD_UP:
if (player->vY < 0)
player->vY = 0;
break;
case DPAD_LEFT:
if (player->vX < 0)
player->vX = 0;
break;
case DPAD_DOWN:
if (player->vY > 0)
player->vY = 0;
break;
case DPAD_RIGHT:
if (player->vX > 0)
player->vX = 0;
break;
default:
break;
}
break;
case SDL_QUIT:
quit = 1;
playing = 0;
break;
default:
break;
}
}
}
int init(int argc, char* args[])
{
int i;
printf("Initializing SDL\n");
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) == -1) return 0;
printf("Initialized SDL\nCreating window\n");
if (argc >= 2)
{
if (strcmp(args[1], "-w"))
{
window = SDL_SetVideoMode(SCREEN_WIDTH*SCALE_FACTOR, SCREEN_HEIGHT*SCALE_FACTOR, 32, SDL_FULLSCREEN|SDL_HWSURFACE);
fullscreen = 1;
}
else window = SDL_SetVideoMode(SCREEN_WIDTH*SCALE_FACTOR, SCREEN_HEIGHT*SCALE_FACTOR, 32, SDL_HWSURFACE|SDL_RESIZABLE);
}
else
{
window = SDL_SetVideoMode(SCREEN_WIDTH*SCALE_FACTOR, SCREEN_HEIGHT*SCALE_FACTOR, 32, SDL_FULLSCREEN|SDL_HWSURFACE);
fullscreen = 1;
}
SDL_ShowCursor(0);
screen = loadImage("assets/rawscreen.png");
if (window == NULL || screen == NULL) return 0;
SDL_WM_SetCaption("Catbug", NULL);
printf("Window created\nInitializing data\n");
if (TTF_Init() == -1) return 0;
ticker = time(NULL);
bg = loadImage("assets/seethruzone.png");
hpwedge = loadImage("assets/hp.png");
2019-03-15 07:13:49 +00:00
font = TTF_OpenFont("assets/kong.ttf", 8);
font2 = TTF_OpenFont("assets/kong.ttf", 16);
2019-02-04 21:24:24 +00:00
player = newCatbug(35, 90);
stuff = malloc(10*sizeof(Pickup*));
for (i = 0; i < 10; i++)
{
stuff[i] = NULL;
}
return 1;
}
void title()
{
SDL_Surface* titletxt = loadImage("assets/titletext.png");
int here = 1;
while (here)
{
timeStart(&fps);
renderBG();
applySurface(0, 0, titletxt, screen, NULL);
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case FS_BUTTON:
toggleFullscreen();
break;
case PAUSE_BUTTON:
here = 0;
quit = 0;
break;
case QUIT_BUTTON:
here = 0;
quit = 1;
playing = 0;
default:
break;
}
break;
case SDL_QUIT:
here = 0;
playing = 0;
quit = 1;
break;
default:
break;
}
}
frameAdvance();
}
SDL_FreeSurface(titletxt);
}
void resetGame()
{
maxHP = STARTING_HP;
player->HP = STARTING_HP;
player->x = 35;
player->y = 90;
player->vX = 0;
player->vY = 0;
points = 0;
threshold = 10;
cleanPickups();
}
void pauseGame()
{
SDL_Color white = {255,255,255};
SDL_Surface* paused = TTF_RenderText_Solid(font,"paused",white);
int here = 1;
while (here)
{
timeStart(&fps);
player->vX = 0;
player->vY = 0;
applySurface(10,30,paused,screen,NULL);
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case FS_BUTTON:
toggleFullscreen();
break;
case PAUSE_BUTTON:
here = 0;
break;
case QUIT_BUTTON:
here = 0;
playing = 0;
quit = 1;
default:
break;
}
break;
case SDL_QUIT:
here = 0;
playing = 0;
quit = 1;
break;
default:
break;
}
}
frameAdvance();
}
SDL_FreeSurface(paused);
}
void timeDilation()
{
int i = getTicks(&fps);
if (i < 1000/30)
SDL_Delay(1000/30 - i);
}
void frameAdvance()
{
scaleScreen();
SDL_Flip(window);
timeDilation();
}
void toggleFullscreen()
{
if (!fullscreen)
{
SDL_SetVideoMode(SCREEN_WIDTH*SCALE_FACTOR, SCREEN_HEIGHT*SCALE_FACTOR, 32, SDL_FULLSCREEN|SDL_HWSURFACE);
fullscreen = 1;
}
else
{
SDL_SetVideoMode(SCREEN_WIDTH*SCALE_FACTOR, SCREEN_HEIGHT*SCALE_FACTOR, 32, SDL_HWSURFACE|SDL_RESIZABLE);
fullscreen = 0;
}
}
void cleanup()
{
if (fullscreen)
toggleFullscreen();
printf("Freeing assets\n");
deleteCatbug(player);
cleanPickups();
free(stuff);
SDL_FreeSurface(bg);
SDL_FreeSurface(hpwedge);
SDL_FreeSurface(score);
SDL_FreeSurface(screen);
SDL_FreeSurface(window);
TTF_CloseFont(font);
TTF_CloseFont(font2);
printf("Closing SDL systems\n");
TTF_Quit();
SDL_Quit();
printf("Cleanup complete\n");
}