LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-02-2006, 11:59 PM   #1
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Rep: Reputation: 30
Can't seem to find reason why case/if ControlsStructs aren't working properly...


Before I program my main game differently, I usualy create a side program so see if what I am doing is going to work. Well, I don't know what is wrong with this program I made, it makes absolutely no sense to me why it is going from one case or if statement to another even though it is not the correct statement. (I believe this is the correct term, correct me if I am wrong.) Here is the code:

Code:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <SDL/SDL.h>
using namespace std;

	SDL_Surface *screen;	

class X_Y
{
	public:
	//Good Guys location, should stay the same.
	int xg;
	int yg;
	int xgl;
	int ygl;

	//Bad Guys location, IS going to be changed in the future.
	int xb;
	int yb;
	int xbl;
	int ybl;

	int x_result;
	int y_result;

	void xyconstruct()
	{
		xg = 0;
		yg = 0;
		xgl = 0;
		ygl = 0;

		xb = 288;
		yb = 128;
		xbl = 9;
		ybl = 4;

		x_result = 0;
		y_result = 0;
	}

}xy;

void move_xy(int MOVE)
{
	cout << endl << MOVE << endl;
	
	switch (MOVE){
		case 1:
			xy.xg = xy.xg - 32;
			cout << endl << xy.xg << " = " << xy.xg << " - 32.\n";
			if(xy.xg > 0){ xy.xg = 0; }
			break;
		case 2:
			xy.xg = xy.xg + 32;
			cout << endl << xy.xg << " = " << xy.xg << " + 32.\n";
			break;
		default:
		cout << "\n\tSomething fucked up as usual\n";
	}
		
	
}
		

int main(int nNumberofArgs, char* pszArgs[])
{	
	if(SDL_Init(SDL_INIT_VIDEO)==-1)
	{
		printf("\n\tCouldn't initialize Init_SDL(): %s", SDL_GetError());
		exit(-1);
	}

	xy.xyconstruct();

	screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);	

	bool done = 0;
	
	//No need for this.
	int DIRECTION;

	SDL_Event event;
	
	do{
		while(SDL_PollEvent(&event)){
			switch(event.type){
				case SDL_KEYDOWN:
				switch(event.key.keysym.sym){
					case SDLK_LEFT:
						move_xy(1);
					case SDLK_RIGHT:
						move_xy(2);
					case SDLK_UP:
						//NOT IMPLEMENTED YET!
						break;
					case SDLK_DOWN:
						//NOT IMPLEMENTED YET!
						break;					
					case SDLK_ESCAPE:
						done = 1;
						break;
					default:
						printf("\n\tThis wasn't supposed to happened: Check line 62\n");
						break;
				}
				case SDL_KEYUP:
					switch (event.key.keysym.sym){
						case SDLK_LEFT:
							break;
						case SDLK_RIGHT:
							break;
						case SDLK_UP:
							break;
						case SDLK_DOWN:
							break;
					}
				}
			}
		}while(done == 0);
	SDL_Quit();

return 0;
}
I have tried using a if control structure but with the same outcome. It starts up just fine, but when I press the left key, it does not seem to want to subtract 32 from xy.xg. This does not seem complicated to me, but it frustrates me when things as simple as above should go smooth don't.

Last edited by RHLinuxGUY; 05-03-2006 at 12:02 AM.
 
Old 05-03-2006, 12:52 AM   #2
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
Code:
case SDLK_LEFT:
	move_xy(1);
case SDLK_RIGHT:
	move_xy(2);
You forgot the break-statements.
Tip: always compile with -Wall -Wextra. As far as I know, gcc will warn you about such things.
 
Old 05-03-2006, 01:09 AM   #3
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889

Original Poster
Blog Entries: 1

Rep: Reputation: 30
O wow, that is pretty pathetic. Thank you addy86!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Could there be a reason my OpenGL is working so badly? Chairmonkey Linux - Hardware 9 12-03-2005 06:53 PM
How can I find out the reason of unexpected reboot (redhat3+2.6.11)? yuzuohong Linux - General 2 05-24-2005 05:49 PM
some reason java stop working Paxmaster Linux - Software 1 01-12-2005 10:35 PM
how to find out the reason when the system stops shigeru Linux - General 3 07-01-2004 08:08 PM
makeing Find command not case sensitive? -ilname isnt working. systemgsr Linux - General 2 12-25-2001 12:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration