Code: Select all
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
int main (int argc, char *argv[])
{
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int TILE_WIDTH = 32;
const int TILE_HEIGHT = 32;
bool done = false;
bool output = false;
bool topLeftCorner = false;
bool bottomRightCorner = true;
int mouseX = 0;
int mouseY = 0;
int x = 0;
int y = 0;
int w = SCREEN_WIDTH;
int h = SCREEN_HEIGHT;
int topLeftY = 0;
int topLeftX = 0;
int bottomRightY = 0;
int bottomRightX = 0;
SDL_Surface *buffer = NULL;//creates the buffer
SDL_Surface *grass = NULL;//creates the buffer
SDL_Init(SDL_INIT_VIDEO);
buffer = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_SWSURFACE);
SDL_Event event;
SDL_WM_SetCaption("Tile", NULL);
grass = IMG_Load("grass2.png");
while(!done)
{
if( event.type == SDL_MOUSEBUTTONDOWN)
{
//Get the mouse offsets
mouseX = event.motion.x;
mouseY = event.motion.y;
if (topLeftCorner == false)
{
topLeftX = mouseX;
topLeftY = mouseY;
printf ("tlX is %d tlY is %d \n", topLeftX,topLeftY);
topLeftCorner = true;
}
if (bottomRightCorner == false )
{
bottomRightX = mouseX;
bottomRightY = mouseY;
printf ("brX is %d brY is %d \n", bottomRightX,bottomRightY);
printf ("tlX is %d tlY is %d \n", topLeftX,topLeftY);
bottomRightCorner = true;
}
if (output == false)
{
output = true;
printf ("mouseX is %d mouseY is %d \n", mouseX,mouseY);
}
event.type = NULL;
}
SDL_FillRect(buffer,NULL,0x000000);
for (int i = y; i <h; i+=TILE_HEIGHT)
{
for (int j = x; j<w; j+=TILE_WIDTH)
{
SDL_Rect area = { j, i, TILE_WIDTH, TILE_HEIGHT};
SDL_BlitSurface( grass, NULL,buffer , &area );
}
}
SDL_Flip(buffer);
Uint8 *key = SDL_GetKeyState (NULL);
if (key[SDLK_ESCAPE])
{
done = true;
}
if (key[SDLK_l])
{
grass = IMG_Load("grass2.png");
}
if (key[SDLK_s])
{
//* this is testing code to see if the math is right
//will not work when topLeftY is at 80.
topLeftX = 0;
topLeftY = 40;
bottomRightX = 132;
bottomRightY = 100;
//*/
x = topLeftX;
y = topLeftY;
w = (bottomRightX - topLeftX);
h = (bottomRightY - topLeftY);
}
if (key[SDLK_c])
{
if (topLeftCorner == true && bottomRightCorner == true)
{
bottomRightCorner = false;
}
output = false;
}
while(SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
done = true;
}
}
}
SDL_FreeSurface(grass);
SDL_Quit();
return 0;
}