2012-12-31 22:58:44 +00:00
|
|
|
#include "main.h"
|
|
|
|
|
2013-01-09 05:36:22 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2012-12-31 22:58:44 +00:00
|
|
|
namespace scrnfunk
|
|
|
|
{
|
|
|
|
// Optimize image on load to reduce processing.
|
|
|
|
SDL_Surface *load_image(const char *file)
|
|
|
|
{
|
|
|
|
SDL_Surface *image_format;
|
|
|
|
SDL_Surface *screen_format;
|
|
|
|
|
|
|
|
// Load image
|
|
|
|
image_format = IMG_Load(file);
|
|
|
|
|
|
|
|
/*
|
|
|
|
DOES THE FILE EVEN EXIST?
|
|
|
|
*/
|
|
|
|
|
|
|
|
if(image_format != 0)
|
|
|
|
{
|
|
|
|
// Optimize
|
|
|
|
screen_format = SDL_DisplayFormat(image_format);
|
|
|
|
|
|
|
|
// Release old image
|
|
|
|
SDL_FreeSurface(image_format);
|
|
|
|
}
|
|
|
|
|
|
|
|
return screen_format;
|
|
|
|
}
|
|
|
|
|
2013-01-01 00:03:32 +00:00
|
|
|
void apply_image(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip)
|
2012-12-31 22:58:44 +00:00
|
|
|
{
|
|
|
|
SDL_Rect pos;
|
|
|
|
|
|
|
|
pos.x = x;
|
|
|
|
pos.y = y;
|
|
|
|
|
|
|
|
SDL_BlitSurface(source, clip, destination, &pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removes color from an image.
|
|
|
|
bool RemoveColor(SDL_Surface* image, Uint8 R, Uint8 G, Uint8 B)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
|
|
|
// Map color on the image
|
|
|
|
Uint32 color = SDL_MapRGB(image->format, R, G, B);
|
|
|
|
|
|
|
|
// Remove color from the image.
|
|
|
|
result = SDL_SetColorKey(image, SDL_SRCCOLORKEY, color);
|
|
|
|
|
|
|
|
if(!result)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace scrns
|
|
|
|
{
|
2013-01-09 05:36:22 +00:00
|
|
|
// Returns a pointer on success, NULL on an error
|
2013-01-14 02:21:15 +00:00
|
|
|
SDL_Surface *initiateSDL(int x, int y)
|
2012-12-31 22:58:44 +00:00
|
|
|
{
|
2013-01-14 02:21:15 +00:00
|
|
|
SDL_Surface *screen = NULL;
|
2012-12-31 22:58:44 +00:00
|
|
|
SDL_Surface *vid_init;
|
2013-03-04 11:31:47 +00:00
|
|
|
int init;//, ttf_ret;
|
2012-12-31 22:58:44 +00:00
|
|
|
|
|
|
|
// Initiate SDL and check
|
|
|
|
init = SDL_Init(SDL_INIT_VIDEO);
|
|
|
|
if(init < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Unable to initiate SDL: %s\n", SDL_GetError());
|
2013-01-01 06:53:58 +00:00
|
|
|
return NULL;
|
2012-12-31 22:58:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Set video and check
|
|
|
|
vid_init = SDL_SetVideoMode(x, y, 32, SDL_SWSURFACE);
|
|
|
|
if(vid_init == 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Video initialization failed: %s", SDL_GetError());
|
2013-01-01 06:53:58 +00:00
|
|
|
return NULL;
|
2012-12-31 22:58:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get video surface and check
|
|
|
|
screen = SDL_GetVideoSurface();
|
2013-01-01 06:53:58 +00:00
|
|
|
if(screen == NULL)
|
2012-12-31 22:58:44 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to get video surface: %s", SDL_GetError());
|
2013-01-01 06:53:58 +00:00
|
|
|
return NULL;
|
2012-12-31 22:58:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set window title
|
2013-01-02 04:32:56 +00:00
|
|
|
SDL_WM_SetCaption(WINDOW_TITLE " " VERSION, 0);
|
2012-12-31 22:58:44 +00:00
|
|
|
|
2013-03-04 11:31:47 +00:00
|
|
|
/*
|
2012-12-31 22:58:44 +00:00
|
|
|
// Initialize SDL_ttf and check
|
|
|
|
ttf_ret = TTF_Init();
|
|
|
|
if(ttf_ret == -1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "True Type Font initialization failed: %s", SDL_GetError());
|
2013-01-01 06:53:58 +00:00
|
|
|
return NULL;
|
2012-12-31 22:58:44 +00:00
|
|
|
}
|
2013-03-04 11:31:47 +00:00
|
|
|
*/
|
2013-01-09 05:36:22 +00:00
|
|
|
|
|
|
|
SDL_FreeSurface(vid_init);
|
2012-12-31 22:58:44 +00:00
|
|
|
|
2013-01-01 06:53:58 +00:00
|
|
|
// Success
|
|
|
|
return screen;
|
2012-12-31 22:58:44 +00:00
|
|
|
}
|
|
|
|
|
2013-03-04 11:31:47 +00:00
|
|
|
/*SDL_Surface *MainMenu()
|
|
|
|
{
|
|
|
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
SDL_Surface *Stage1()
|
2012-12-31 22:58:44 +00:00
|
|
|
{
|
|
|
|
// Screen
|
2013-01-09 05:36:22 +00:00
|
|
|
SDL_Surface *screen = NULL;
|
|
|
|
SDL_Surface *background = NULL;
|
2012-12-31 22:58:44 +00:00
|
|
|
|
|
|
|
// Image filenames
|
2013-03-04 11:31:47 +00:00
|
|
|
const char *background_image = "bin/stage.bmp";
|
2013-01-01 06:53:58 +00:00
|
|
|
|
2013-03-04 11:31:47 +00:00
|
|
|
screen = initiateSDL(900, 646);
|
2012-12-31 22:58:44 +00:00
|
|
|
|
2013-01-01 06:53:58 +00:00
|
|
|
if(screen == NULL)
|
2012-12-31 22:58:44 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Start up failure.\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Load images
|
|
|
|
background = scrnfunk::load_image(background_image);
|
2013-03-04 11:31:47 +00:00
|
|
|
|
2012-12-31 22:58:44 +00:00
|
|
|
// Blit background image
|
|
|
|
scrnfunk::apply_image(0, 0, background, screen);
|
2013-01-03 00:37:58 +00:00
|
|
|
|
|
|
|
// Update screen with all applied images
|
2012-12-31 22:58:44 +00:00
|
|
|
if(screen != 0)
|
|
|
|
SDL_Flip(screen);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "Couldn't update screen.\n");
|
|
|
|
}
|
2013-03-04 11:31:47 +00:00
|
|
|
|
|
|
|
// Free old surfaces
|
|
|
|
SDL_FreeSurface(background);
|
|
|
|
SDL_FreeSurface(screen);
|
|
|
|
|
2012-12-31 22:58:44 +00:00
|
|
|
return screen;
|
|
|
|
}
|
2013-03-04 11:31:47 +00:00
|
|
|
|
|
|
|
SDL_Surface *Movement(SDL_Surface *screen)
|
2013-01-09 05:36:22 +00:00
|
|
|
{
|
2013-03-04 11:31:47 +00:00
|
|
|
// Movement
|
|
|
|
// D -- Mario move LEFT
|
|
|
|
// A -- Mario move RIGHT
|
2013-01-11 19:32:15 +00:00
|
|
|
|
2013-01-14 02:21:15 +00:00
|
|
|
SDL_Surface *background = NULL;
|
2013-03-04 11:31:47 +00:00
|
|
|
SDL_Surface *mario0 = NULL;
|
|
|
|
//SDL_Surface *mario1 = NULL;
|
|
|
|
//SDL_Surface *mario2 = NULL;
|
|
|
|
SDL_Surface *mario3 = NULL;
|
|
|
|
//SDL_Surface *mario4 = NULL;
|
|
|
|
//SDL_Surface *mario5 = NULL;
|
|
|
|
|
|
|
|
int x,y;
|
|
|
|
x = 125;
|
|
|
|
y = 520;
|
2013-01-14 02:21:15 +00:00
|
|
|
|
|
|
|
// Image filenames
|
2013-03-04 11:31:47 +00:00
|
|
|
const char *background_image = "bin/stage.bmp";
|
|
|
|
const char *mario_image0 = "bin/IMG0.bmp";
|
|
|
|
//const char *mario_image1 = "bin/IMG1.bmp";
|
|
|
|
//const char *mario_image2 = "bin/IMG2.bmp";
|
|
|
|
const char *mario_image3 = "bin/IMG3.bmp";
|
|
|
|
//const char *mario_image4 = "bin/IMG4.bmp";
|
|
|
|
//const char *mario_image5 = "bin/IMG5.bmp";
|
|
|
|
|
2013-01-14 02:21:15 +00:00
|
|
|
background = scrnfunk::load_image(background_image);
|
2013-03-04 11:31:47 +00:00
|
|
|
mario0 = scrnfunk::load_image(mario_image0);
|
|
|
|
//mario1 = scrnfunk::load_image(mario_image1);
|
|
|
|
//mario2 = scrnfunk::load_image(mario_image2);
|
|
|
|
mario3 = scrnfunk::load_image(mario_image3);
|
|
|
|
//mario4 = scrnfunk::load_image(mario_image4);
|
|
|
|
//mario5 = scrnfunk::load_image(mario_image5);
|
|
|
|
|
|
|
|
SDL_Rect clip;
|
|
|
|
|
|
|
|
clip.x = x;
|
|
|
|
clip.y = y;
|
|
|
|
clip.w = 100;
|
|
|
|
clip.h = 100;
|
|
|
|
|
|
|
|
scrnfunk::RemoveColor(mario0, 0x00, 0x00, 0xFF); // Blue
|
|
|
|
//scrnfunk::RemoveColor(mario1, 0x00, 0x00, 0xFF); // Blue
|
|
|
|
//scrnfunk::RemoveColor(mario2, 0x00, 0x00, 0xFF); // Blue
|
|
|
|
scrnfunk::RemoveColor(mario3, 0x00, 0x00, 0xFF); // Blue
|
|
|
|
scrnfunk::apply_image(x, y, mario0, screen);
|
|
|
|
|
|
|
|
if(screen != 0)
|
|
|
|
SDL_Flip(screen);
|
|
|
|
|
|
|
|
SDL_Event event;
|
2013-01-09 05:36:22 +00:00
|
|
|
|
2013-03-04 11:31:47 +00:00
|
|
|
while(1)
|
2013-01-09 05:36:22 +00:00
|
|
|
{
|
2013-01-11 19:32:15 +00:00
|
|
|
while(SDL_PollEvent(&event))
|
2013-01-09 05:36:22 +00:00
|
|
|
{
|
2013-01-11 19:32:15 +00:00
|
|
|
if(event.type == SDL_QUIT)
|
2013-01-14 02:21:15 +00:00
|
|
|
return NULL;
|
2013-03-04 11:31:47 +00:00
|
|
|
|
2013-01-11 19:32:15 +00:00
|
|
|
else if(event.type == SDL_KEYDOWN)
|
2013-01-09 05:36:22 +00:00
|
|
|
{
|
2013-03-04 11:31:47 +00:00
|
|
|
if(event.key.keysym.sym == SDLK_d) // RIGHT
|
|
|
|
{
|
|
|
|
scrnfunk::apply_image(x, y, background, screen, &clip); // Cover current Mario
|
|
|
|
x=x+10;
|
|
|
|
clip.x = x;
|
|
|
|
scrnfunk::apply_image(x, y, mario3, screen); // Place new Mario
|
2013-01-11 19:32:15 +00:00
|
|
|
}
|
2013-03-04 11:31:47 +00:00
|
|
|
else if(event.key.keysym.sym == SDLK_a) // LEFT
|
2013-01-11 19:32:15 +00:00
|
|
|
{
|
2013-03-04 11:31:47 +00:00
|
|
|
scrnfunk::apply_image(x, y, background, screen, &clip); // Cover current Mario
|
|
|
|
x=x-10;
|
|
|
|
clip.x = x;
|
|
|
|
scrnfunk::apply_image(x, y, mario3, screen); // Place new Mario
|
2013-01-11 19:32:15 +00:00
|
|
|
}
|
2013-01-09 05:36:22 +00:00
|
|
|
}
|
|
|
|
}
|
2013-01-14 02:21:15 +00:00
|
|
|
|
2013-03-04 11:31:47 +00:00
|
|
|
if(screen != 0)
|
|
|
|
SDL_Flip(screen);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "Couldn't update screen.\n");
|
2013-01-14 02:21:15 +00:00
|
|
|
}
|
2013-03-04 11:31:47 +00:00
|
|
|
|
2013-01-14 02:21:15 +00:00
|
|
|
return screen;
|
2013-01-09 05:36:22 +00:00
|
|
|
}
|
2013-01-11 19:32:15 +00:00
|
|
|
}
|