Code: Select all
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
int main (int argc, char *argv[])
{
//CONSTANTS
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int BITS_PER_PIXEL = 32;
const int TILE_WIDTH = 32;
const int TILE_HEIGHT = 32;
SDL_Surface *buffer = NULL;//creates the buffer
SDL_Surface *tileSheet = NULL;//inititates tileSheet surface
SDL_Surface *water = NULL;//inititates water surface
SDL_Surface *desert = NULL;//inititates desert surface
SDL_Surface *grass = NULL;//inititates grass surface
tileSheet =IMG_Load("blank.png");//128x128 image
water = IMG_Load("water.png");//128x128 image
desert = IMG_Load("desert.png");//128x128 image
grass = IMG_Load("grass.png");//128x128 image
Uint32 colorkey = SDL_MapRGB( tileSheet->format, 0xff, 0, 0xFF );
SDL_SetColorKey( tileSheet, SDL_SRCCOLORKEY, colorkey );
if (!water)
return 1;
if (!desert)
return 1;
if (!grass)
return 1;
if(!tileSheet)
return 1;
int counter_a = 0;
int mouse_x = 0;
int mouse_y = 0;
int selectedBox[3]= {0,0,0};
SDL_Event event; //creates an event
bool done = false; //initializes done
bool gKey = false;//initializes gkey
bool grid = false;//initializes grid
SDL_Init(SDL_INIT_VIDEO); //inititate SDL
buffer = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, BITS_PER_PIXEL, SDL_SWSURFACE); //sets the buffer to the screen
if(!buffer)
return 1;
SDL_WM_SetCaption("Window", NULL); //sets the window caption
while(!done)//while done is false
{
SDL_FillRect(buffer,NULL,0x000000); //fills the buffer with a black retangle
SDL_Rect area = {(SCREEN_WIDTH -(TILE_WIDTH*4)), (SCREEN_HEIGHT -(TILE_HEIGHT*4)),(TILE_WIDTH*4),(TILE_HEIGHT*4)}; //{x,y,w,h}
SDL_FillRect(buffer,&area,0xBBBBBB);//fills a rectangle to the buffer at area coordinates, and 0xBBBBBB colour
Uint8 *key = SDL_GetKeyState (NULL); //allows us to get a keystate
if( event.type == SDL_MOUSEMOTION )
{ //Get the mouse offsets
mouse_x = event.motion.x;
mouse_y = event.motion.y; //If the mouse is over the button
}
for (int j = 1; j < 5; j++) //for loop for the rows
{
for (int i = 1; i < 5; i++) //for loop for the tiles in rows
{
counter_a ++; //counts the loop
SDL_Rect sheet = {(SCREEN_WIDTH -(TILE_WIDTH*5-(TILE_WIDTH*i))),(SCREEN_HEIGHT -(TILE_HEIGHT*j)),TILE_WIDTH,TILE_HEIGHT}; //sets box specifications
if( ( mouse_x > sheet.x ) && ( mouse_x < sheet.x + sheet.w ) && ( mouse_y > sheet.y ) && ( mouse_y < sheet.y + sheet.h ) )
//if the mouse is in the box
{
if( event.type == SDL_MOUSEBUTTONDOWN ) // if a mouse button is down
{
selectedBox[0] = counter_a; //moves the couter a variable out of loop
selectedBox[1] = j;
selectedBox[2] = i;
}
}
}
}
SDL_BlitSurface( tileSheet, NULL,buffer , &area ); //puts the tilesheet on the screen
if (key[SDLK_ESCAPE]) //if escape was pressed
done = true; //exits loop
if (key[SDLK_BACKSPACE]) //if backspace was pressed
selectedBox[0] = 0; //sets the selected box to 0
if (key[SDLK_1])
tileSheet = grass;
if (key[SDLK_2])
tileSheet = water;
if (key[SDLK_3])
tileSheet = desert;
if (key[SDLK_g]) //if g key is pressed
{
if (gKey == false) //if gKey is false
{
gKey = true;//set gkey to true
if (grid == true) //if the grid is on the screen
{
grid = false; //toggles the grid
}
else if (grid == false) //if the grid is on the screen
{
grid = true; //toggles the grid
}
}
}
else //if any other key is pressed, or no key is pressed
{
gKey = false; //set gkey to false
}
if (grid == true) //if we put the grid on
{
for (int i = SCREEN_WIDTH; i >=0; i-=TILE_WIDTH)
//loop to the whole screen, incrementing by the tile width
{
SDL_Rect grid_vertical = {i, 0, 2, SCREEN_HEIGHT}; //{x,y,w,h}
SDL_FillRect(buffer,&grid_vertical,0xAAAAAA);
//adds a vertical box at grid_vertical location
}
for (int i = SCREEN_HEIGHT; i >= 0; i-=TILE_HEIGHT)
//loop in the horizontal direction
{
SDL_Rect grid_horizontal = {0, i, SCREEN_WIDTH, 2}; //x,y,w,h
SDL_FillRect(buffer,&grid_horizontal,0xAAAAAA);//adds a horizontal box, at grid_horizontal location
}
}
if (selectedBox[0] >0)
{
//Selection Box
SDL_Rect sheet_top = {(SCREEN_WIDTH -(TILE_WIDTH*5-(TILE_WIDTH*selectedBox[2]))),(SCREEN_HEIGHT -(TILE_HEIGHT*selectedBox[1])),TILE_WIDTH,2};//top
SDL_Rect sheet_bottom = {(SCREEN_WIDTH -(TILE_WIDTH*5-(TILE_WIDTH*selectedBox[2]))),(SCREEN_HEIGHT -(TILE_HEIGHT*(selectedBox[1]-1))),TILE_WIDTH,2};//bottom
SDL_Rect sheet_left = {(SCREEN_WIDTH -(TILE_WIDTH*5-(TILE_WIDTH*selectedBox[2]))),(SCREEN_HEIGHT -(TILE_HEIGHT*selectedBox[1])),2,TILE_HEIGHT};//left
SDL_Rect sheet_right = {(SCREEN_WIDTH -(TILE_WIDTH*5-(TILE_WIDTH*(selectedBox[2]+1)))),(SCREEN_HEIGHT -(TILE_HEIGHT*(selectedBox[1]))),2,TILE_HEIGHT+2};//right
SDL_FillRect(buffer,&sheet_top,0x00FF00);
SDL_FillRect(buffer,&sheet_bottom,0x00FF00);
SDL_FillRect(buffer,&sheet_left,0x00FF00);
SDL_FillRect(buffer,&sheet_right,0x00FF00);
}
SDL_Flip(buffer); //flips the buffer
while(SDL_PollEvent(&event)) //if there is an event
{
if (event.type == SDL_QUIT)//if the program was xed out
{
done = true;//exit main loop
}
}
}
SDL_FreeSurface(tileSheet);
SDL_FreeSurface(grass);
SDL_FreeSurface(water);
SDL_FreeSurface(desert);
SDL_Quit();//exit SDL
return 0;
}