Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL/SDL.h>
#include <windows.h>
#include <string>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
SDL_Surface *screen = NULL;
SDL_Surface *background = NULL;
SDL_Surface *character = NULL;
SDL_Surface *town = NULL;
SDL_Surface *weapshoptemp = NULL;
SDL_Surface *rustysword = NULL;
SDL_Surface *bronzesword = NULL;
SDL_Surface *plasmasword = NULL;
SDL_Surface *platinumsword = NULL;
SDL_Surface *silversword = NULL;
SDL_Surface *bdiamondsword = NULL;
SDL_Surface *linea = NULL;
SDL_Color textColor = { 255, 255, 255 };
SDL_Event event;
TTF_Font *font = NULL;
int location =2;
int initsdl()
{
/* Initialize SDL */
if (SDL_Init (SDL_INIT_EVERYTHING) == -1)
{
return 1;
}
/* Set 640x480 16-bits video mode */
screen = SDL_SetVideoMode (640, 480, 16, SDL_SWSURFACE |
SDL_DOUBLEBUF);
if (screen == NULL)
{
return 1;
}
if( TTF_Init() == -1 )
{
return false;
}
SDL_WM_SetCaption ("Starting with physics", NULL);
}
int freesurfaces()
{
SDL_FreeSurface(background);
SDL_FreeSurface(screen);
SDL_FreeSurface(character);
SDL_FreeSurface(rustysword);
SDL_FreeSurface(bronzesword);
SDL_FreeSurface(silversword);
SDL_FreeSurface(plasmasword);
SDL_FreeSurface(platinumsword);
SDL_FreeSurface(bdiamondsword);
SDL_FreeSurface(linea);
}
void loadimages()
{
character = loadimage("ani.gif");
rustysword = loadimage("rustysword.png");
bronzesword = loadimage("bronzesword.png");
silversword = loadimage("silversword.png");
plasmasword = loadimage("plasmasword.png");
platinumsword = loadimage("platinumsword.png");
bdiamondsword = loadimage("bluediamondsword.png");
font = TTF_OpenFont( "achafsex.ttf", 28 );
}
void displayarea()
{
//1 is town
if(location == 1)
{
background = loadimage("town.png");
applysurface(0,0, background, screen, NULL);
applysurface(x, y, character, screen, &animation[1]);
}
else if(location == 2)
{
background = loadimage("shop.png");
applysurface(0,0, background, screen, NULL);
applysurface(10,70, rustysword, screen, NULL);
applysurface(310,70, bronzesword, screen, NULL);
applysurface(10, 140, silversword, screen, NULL);
applysurface(310, 140, platinumsword, screen, NULL);
applysurface(10, 210, bdiamondsword, screen, NULL);
applysurface(310, 210, plasmasword, screen, NULL);
}
}
void play()
{
int MouseCoordX, MouseCoordY;
displayarea();
bool quit = false;
while(quit == false)
{
while(SDL_PollEvent(&event))
{
displayarea();
if(event.type == SDL_QUIT)
{
quit = true;
}
else if(location == 1)
{
if( event.type == SDL_KEYDOWN )
{
switch( event.key.keysym.sym )
{
case SDLK_UP: moveup();
break;
case SDLK_LEFT: moveleft();
break;
case SDLK_RIGHT: moveright();
break;
case SDLK_DOWN: movedown();
}
}
}
else if(location == 2)
{
if(event.type == SDL_MOUSEBUTTONDOWN)
{
if(event.button.button == SDL_BUTTON_LEFT)
{
MouseCoordX = event.button.x;
MouseCoordY = event.button.y;
SDL_FreeSurface(linea);
//if I comment the following line the program runs else it stops and
does not even apply the line applysurface that follows it.
linea = TTF_RenderText_Solid( font, "Welcome to my shop we have 6 weapons", textColor );
applysurface(MouseCoordX, MouseCoordY, linea, screen, NULL);
}
}
}
SDL_Flip(screen);
}
}
}
int main (int argc, char *argv[])
{
initsdl();
loadimages();
play();
freesurfaces();
return 0;
}
sorry I know its long, but it was the shortest version of the real code I could copy paste and still give you an idea of whats goin on