Jump to content

  • Log In with Google      Sign In   
  • Create Account

JustinDaniel

Member Since 11 Sep 2011
Offline Last Active Sep 25 2013 10:46 PM

#4927744 c++ SDL Button class

Posted by JustinDaniel on 02 April 2012 - 08:56 PM

The problem is, the event is not processing properly, You need to check when the user clicks the button and quickly respond to it.

Probably write a function to wrap the mouse intersecting and then check if the button is being pressed and if the mouse is on top of whatever object.
bool IsOnTop(int objectX, int objectY, int objectW, int objectH, int mouseX, int mouseY )
{
	if( mouseX < objectX || mouseY < objectY )
	{
		return false;
	}
	if( mouseX > objectX+objectW || mouseY > objectY+objectH )
	{
		return false;
	}

	return true;
}

//Then while handling events..
//Store a variable for the SDL_BUTTON_LEFT key

const Uint8 Button = SDL_BUTTON_LEFT;

//While handling events
if(event.type == SDL_MOUSEBUTTONDOWN )
	{
   	 if( (event.button.button &Button) == Button && intersects(Buttonrect.x, Buttonrect.y, Buttonrect.w, Buttonheight.h, event.button.x, event.button.y))
   	 {
	   	 return true; //Do the things you want when the mouse is clicked here.
   	 }
	}
}

And another question while i'm anyway starting a thread. Do i really have to pass in the screen surface to the button. Are there no way to make the screen variable global somehow. Seems dumb to do it this way

There is one way.
You could pass
SDL_GetVideoSurface();

In the place of SDL_Surface* destination or whatever without making the screen variable a global one

SDL_GetVideoSurface() returns a pointer to the current screen, i.e the main screen you set is returned as a pointer by the SDL_GetVideoSurface()


#4922493 simple cube creation using opengl Qt

Posted by JustinDaniel on 15 March 2012 - 11:47 PM

That is because you are rotating only in the Y- direction, Try changing to Z or X direction or both.
glRotatef(angle, 1, 0, 0);
// or
glRotatef(angle. 0, 0, 1);



#4922238 simple cube creation using opengl Qt

Posted by JustinDaniel on 15 March 2012 - 06:05 AM

What you see in the computer is Only 2D so, if you have placen the cube straight ahead you will only see a Square, To see the 3D shape probably try using
glRotatef( 45, 0, 1, 0 );

That will bring a 3D looking model ;)


#4878499 Game programming vs Girls

Posted by JustinDaniel on 30 October 2011 - 01:24 AM

I met her at a coffee shop, She's not far from my house. But i would love if she was my 3D model posing for a beach volley ball game. Haha.


#4878496 Countries with best food

Posted by JustinDaniel on 30 October 2011 - 01:18 AM

Mexico. I mean it


#4878491 Game programming vs Girls

Posted by JustinDaniel on 30 October 2011 - 12:56 AM

Well I guess you could make a 3D model of her? Share both worlds.


Wish i was a great artist, You know.


#4878488 Game programming vs Girls

Posted by JustinDaniel on 30 October 2011 - 12:48 AM

I know this sounds a bit awkward, But it's true for me that i always get distracted. Well i'm 18 and its common that i am in love with a girl, I am too crazy about her that i can't even program properly, It's too complicated when girls are in something. The problem is i haven't got to talk with her, I keep thinking about her and it's like whenever i try to program or think about ideas of making a game, She pops up like a angel into my mind and i completely stop all the work and go to my Dream land. It's too absurd, i have tried everything not too but yeah it always happens.
Right now i just cant think of anything and i am still doubtful that im posting this now.
Is this happened to any of you guys? If so what you did to keep her out of mind when programming.
I know this is funny, But i would bet it would have happened to someone who is a "Teen".


#4876318 Hi guys , just joined the community

Posted by JustinDaniel on 24 October 2011 - 08:39 AM

Welcome Jack!
First off, I'd assure you that you will love this community and our fellow members.
Since you're here for the first time i'd reckon you grasp in all the topics and threads. You can gain a lot of information about GameDevelopment :)

Anyways for a beginner programming language, C# would be a Kick start for you.
C# is a easy, powerful and strong language.
I would also recommend Python which is also a beginner language which is great i would also recommend you trying Python, Its all up to you. You can choose your own language.
For C# you could use XNA and for Python you could use Pygame.
There are lots of posts about what language to choose. You can go on search in our forums.

Have fun Game devv'ing.


#4874921 A few Questions

Posted by JustinDaniel on 20 October 2011 - 10:40 PM

In programming there are many languages any language is good, For you since you only have used VB6 i would recommend starting with a easy language. I would recommend C# as a starting language as it is way more easy, Many people would say the same answers but, My advice would be "NEVER" use C++ as your first language, I first chose it and unfortunately it was too difficult for me, I had to drop using c++ and i went back learning C# and then a bit Python and then came back to C++ after 2 years and here i am still seeking help sometimes on GameDev.net, I choose the wrong path, I don't want you to do it. Choose which suits for you, try grasping through gamedev's forums and you can get a lot of information about which language to choose and there are many articles which help you guide through the best language to choose, As far as i know i would not recommend C++.

What should language to start with?

As i said before any language except C++ would do great, Here's some languages choose from it.
C#, Python, Delphi, Java, Ruby, well i don't know a lot programming languages, Don't worry other people might help you with it.

my friend wants to start programming paticulary for games

If you want to choose the path for game programming i would say there are many libraries to use. As for now id reckon that using XNA with C# and Pygame with Python would be the best, it's best on which language you choose.
Never forget this, If you're game programming, never lose patience, And don't jump into game programming until you are sure that you know all the basics of programming of that language. Always try to make your game sweet and also short. Don't try to jump in and start to think of making the most awesome 3D game in the world and all you end up is with a junk of half used code, So never forget all the indie games and all the 2D games, It's fun to make and also it makes the users the feeling that they have made a game and even though it looks a bit weird to others it might look like precious gold to you. You have spent a lot of time on it and you feel how precious it is.

any ideas for books?(i gotta get him a birthday gift)

I think i misunderstood your post, Are you trying to learn game programming or your friend is, I'm sorry i think i didn't read, if so make your friend read this post.
Anyways for books, there are many local book stores which sell books regarding programming languages and also gaming, I'm afraid i don't know a lot about books, i reckon you wait for my GameDev friends to reply for books. Sorry.

Peace and good luck in your programming.


#4872744 Help with jumping

Posted by JustinDaniel on 14 October 2011 - 09:50 PM

Well, i should not be doing this, but uh, it makes me mad how people learn to program games when they dont even know basics of c++ or whatever language it might be, copying and pasting code doesnt get you anywhere, if you have to become a game programmer then you gotta work for it. Asking for help is not bad but asking others to write code for you is totally annoying especially in programming.
#include <sdl.h>
#include <iostream>
#include <sdl_image.h>
#include <string>
#include <cstdlib>
#include <ctime>

int main( int argc, char* argv[] )
{
	std::srand( std::time(0) );

	if( SDL_Init( SDL_INIT_VIDEO ) == -1 )
	{
		std::cout << "Failed  to initialize" << SDL_GetError() << "\n";
		return false;
	}
	std::atexit(&SDL_Quit);

	SDL_Surface *screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );
	if( screen == NULL )
	{
		std::cout << " Failed to set video " << SDL_GetError() << "\n";
		return false;
	}

	SDL_Surface *square = IMG_Load( "square.bmp" );
	if( square == NULL )
	{
		std::cout << "Failed to open square" << SDL_GetError() << "\n";
		return false;
	}

	int x = 20;
	int y = 460;
	int vx = 0;
	int vy = 20;
	int ay = 2;
	bool jumping = false;
	bool quit = false;
	while( !quit )
	{
		SDL_Event event;
		while( SDL_PollEvent( &event ) )
		{
			if( event.type == SDL_KEYDOWN )
			{
				switch( event.key.keysym.sym )
				{
					case SDLK_RIGHT: vx += square->w /3; break;
					case SDLK_LEFT: vx -= square->w/3; break;
					case SDLK_SPACE:
					vy = 20;
					jumping = true;
				}

			}
			else if( event.type == SDL_KEYUP )
			{
				switch( event.key.keysym.sym )
				{
					case SDLK_RIGHT: vx -= square->w /3; break;
					case SDLK_LEFT: vx += square->w /3; break;
					if( jumping )
					{
						if( vy >= -20 )
						{
							vy = vy - ay;
							y = y + vy;
						}
					}
					if( y + square->h > screen->h )
					{
						y = 460;
					}
				}
			}
			if( event.type == SDL_QUIT) 
			{
				quit = true;
			}
		}
		SDL_Rect rect = {x, y, square->w, square->h };

		x += vx;

		if( jumping )
		{
			y -= vy;
		}
		if( vy >= -20 )
		{
			vy = vy - ay;
		}
		if( y + square->h > screen->h )
		{
			y = 460;
		}
		SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF,0xFF,0xFF ) );
		SDL_BlitSurface( square, 0, screen, &rect );

		SDL_Flip(screen);
	}
	SDL_FreeSurface( square );
	return 0;
}

I have given you the entire code on how to make a square jump, Learn from it and see what you can do it with your game, Atleast go through some of the code and learn what it does, learn from it, DONT copy paste.
Peace.


#4872224 Help with jumping

Posted by JustinDaniel on 13 October 2011 - 09:54 AM

First off as serapth said before don't use Dev shed, its too outdated, I would prefer VC++ over it. Try changing to it, it has alot of Microsoft support too.
Next For jumping,
You need your code properly structured.
The main aim of jumping is, First you launch the object say 50 m/s the object goes at 50 m/s to a certain height and once it reaches the height you slowly have to decrease its speed by a downward direction also known as Gravity, But a computer doesnt know what a gravity is or something, we know gravity's value is 9.8 m/s, So we define gravity also known as Acceleration in downward direction, so we set variables for it to let the computer understand it.
I just added some new variables to your code, just identify them.
offSet is changed to int x, and int y because you need to calculate its x and y positions at a certain height.
velocity is changed to int xVel, int yVel because you need a velocity in upward direction not left right alone.
I also added int xAccel and yAccel to calculate gravity.
And also a "bool" Jumping to verify its jumping state.
Try to understand from this code, I dont know if it will work. But im sure it'll help you.
Player::Player()
{
	x = 0;
	y = 0;
	xVel = 0;
	yVel = 0;

	yAccel = 5; // For gravity you need a downward accelaration so we set Accel in downward'y' direction
	xAccel = 0;

	Jumping = false; //Jumping status//

	frame = 0;
	status = PLAYER_RIGHT;

	playerTime.start();
}

void Player::handle_events()
{
	//If a key was pressed
	if( event.type == SDL_KEYDOWN )
	{
    	//Set the velocity
    	switch( event.key.keysym.sym )
    	{
        	case SDLK_RIGHT: xVel += 3; break;
        	case SDLK_LEFT: xVel += 3; break;
			case SDLK_SPACE: // suppose the key is space to jump
			yVel = 40; // You launch the player to a speed of 40 //
			Jumping = true; //Then you set jumping status to true //
    	}
	}
	//If a key was released
	else if( event.type == SDL_KEYUP )
	{
    	//Set the velocity
    	switch( event.key.keysym.sym )
    	{
        	case SDLK_RIGHT: xVel -= 3; break;
        	case SDLK_LEFT: xVel += 3; break;
			case SDLK_SPACE:
			if( Jumping )
			{
				if( yVel > -40 ) // If yVel is greater than its given velocity
				{
					yVel = yVel - yAccel; //We decrease yVel slowly by downward accel //
				}
			}
			if( y <= 0 ) // Check if player has reached the ground //
			{
				yVel = 0;
				yAccel = 0;
				//Remember the value of the ground may change according to where you set its position so try changing values if you cant reach ground //
			}
    	}
	}
}

void Player::move()
{
	x += xVel;

	//keep player in bounds //
	if( ( x < 0 ) || ( x + PLAYER_WIDTH > SCREEN_WIDTH ) )
	{
		x -= xVel;
	}
	if( Jumping ) //If we are jumping //
	{
		y -= yVel; // Make sure velocity is upward direction
	}
	if( yVel > -40 ) //Pretty much same
	{
		yVel = yVel - yAccel;
	}
}

I hope you understand what gravity means in C++, its just simply easy if you understand it :)


PARTNERS