trouble with making a makeshift drag and drop system

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
pythip
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Sat Apr 24, 2010 11:25 pm

trouble with making a makeshift drag and drop system

Post by pythip »

I am making a program where you basically make a box, you click a mouse key, and it sets the coordinates for the top left corner, then you press c to enable you to set the bottom left (pressing another mouse key), then if you press s, it displays the box onto the screen. Its a makeshift click and drag system I attempted to make. The math seems to work (when I manually set the variables), but when I click on the screen it doesn't seem to get it right, any help?

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;
}
pythip
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Sat Apr 24, 2010 11:25 pm

Re: trouble with making a makeshift drag and drop system

Post by pythip »

I think the problem might lie in the fact that if the last tile is too big to fit in the square it doesn't draw, I have to make someway to make it so that the program makes you choose the width to be a multiple of the width of the tile.
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Re: trouble with making a makeshift drag and drop system

Post by LeonBlade »

Hey there, I hope this is what you want...
I whipped it up for you in a few minutes :)
Click here to view source on Pastie

You click and drag to make a tiled rectangle.
No need for hot keys, just click and drag.

It's commented very well.
Hope this helps :)
There's no place like ~/
pythip
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Sat Apr 24, 2010 11:25 pm

Re: trouble with making a makeshift drag and drop system

Post by pythip »

wow, looking at the code you produced compared to mine is crazy, I must say good job on the formatting, I'm going to have to base all my projects (at least the formatting part) off of yours. Thank you very much for giving such a high detailed answer, I now not only know that I was way off, but now I can see the proper way to comment. Thanks again!
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Re: trouble with making a makeshift drag and drop system

Post by LeonBlade »

Of course, always happy to help out :)
There's no place like ~/
Post Reply