#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <string>
#include "SpriteSheet.h"
#include <SDL_mixer.h>
#undef main
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
SDL_Surface* screen = NULL;
SDL_Surface* image = NULL;
SDL_Surface* text = NULL;
SpriteSheet* sheet = NULL;
SDL_Event event;
TTF_Font* font;
SDL_Color text_color = {255, 255, 255};
bool init()
{
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
return false;
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
if (screen == NULL)
return false;
if (TTF_Init() == -1)
return false;
SDL_WM_SetCaption("Shooter", NULL);
return true;
}
Uint32 get_pixel32( SDL_Surface *surface, int x, int y )
{
Uint32 *pixels = (Uint32 *)surface->pixels;
return pixels[ ( y * surface->w ) + x ];
}
void put_pixel32( SDL_Surface *surface, int x, int y, Uint32 pixel )
{
Uint32 *pixels = (Uint32 *)surface->pixels;
pixels[ ( y * surface->w ) + x ] = pixel;
}
SDL_Surface *loadImage(std::string filename)
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load(filename.c_str());
if (loadedImage != NULL)
{
optimizedImage = SDL_DisplayFormat(loadedImage);
SDL_FreeSurface(loadedImage);
}
if (optimizedImage != NULL)
{
Uint32 colorKey = get_pixel32(optimizedImage, 0, 0);
SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, colorKey);
}
return optimizedImage;
}
bool load_files()
{
image = loadImage("001-Fighter01.png");
sheet = new SpriteSheet(image, 4, 4, false);
font = TTF_OpenFont("BurstMyBubble.ttf", 28);
if (font == NULL)
return false;
if (image == NULL)
return false;
return true;
}
bool clean_up()
{
SDL_FreeSurface(image);
SDL_FreeSurface(text);
TTF_CloseFont(font);
TTF_Quit();
SDL_Quit();
return true;
}
void blitSurface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* srcrect, SDL_Rect* target)
{
SDL_BlitSurface(source, srcrect, destination, target);
}
int main(int argc, char* args[])
{
bool quit = false;
if (!init())
return 1;
load_files();
text = TTF_RenderText_Solid(font, "lol. Animation.", text_color);
int frame = 0;
int tick = 0;
SDL_Rect message;
message.x = 4;
message.y = 4;
SDL_Rect sprite;
sprite.x = 32;
sprite.y = 32;
while (!quit)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
quit = true;
else if (event.type = SDL_KEYDOWN)
{
switch( event.key.keysym.sym )
{
case SDLK_UP:
sprite.y -= 4;
break;
case SDLK_DOWN:
sprite.y += 4;
break;
case SDLK_LEFT:
sprite.x -= 4;
break;
case SDLK_RIGHT:
sprite.x += 4;
break;
}
}
}
SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0, 0, 0));
blitSurface(4, 4, text, screen, NULL, &message);
tick++;
blitSurface(32, 32, image, screen, sheet->frame(frame), &sprite);
if (tick > 100)
{
tick = 0;
frame++;
}
if (frame > 15)
frame = 0;
if (SDL_Flip(screen) == -1)
return 1;
}
clean_up();
return 0;
}