View Full Version : I need some help with mouse
admirer
2006.09.28, 05:45 AM
I am unable to utilise mouse events.
I tried it like this:
while(SDL_PollEvent(&event))
{
if(event.type == SDL_MOUSEBUTTONDOWN)
SDL_WM_SetCaption("Mouse is down.",0);
}
But nothing is happening...
Can you please tell me simple tutorials describing handling of mouse events in SDL
I am currently working to develop a tic-tac toe.
Please specify something other than SDL documentation.A lot of confusions.Help please.:blink:
Taxxodium
2006.09.28, 06:44 AM
This is what I do in my game
SDL_Event event;
while (!isDone) {
while ( SDL_PollEvent (&event) ) {
switch (event.type) {
case SDL_MOUSEMOTION:
case SDL_MOUSEBUTTONDOWN:
handleMouseEvents(event);
break;
case SDL_KEYDOWN:
handleKeyEvents(event);
break;
case SDL_QUIT:
isDone = true;
break;
default:
break;
}
}
gameLoop();
}
admirer
2006.09.29, 04:55 AM
I am using the Quick CG library for graphics in C++ .Primary aim Tic-Tac-Toe in C++
My main.cpp is here.
#include <cmath>
#include <cstdlib> //for random numbers: rand();
#include <SDL/SDL.h>
#include "QuickCG.h"
#include "Q3DMath.h"
typedef struct cord
{
int x1;
int x2;
int y1;
int y2;
}cord;
typedef struct board
{
cord crd[9];
}brd;
int main(int argc, char *argv[])
{
void play(brd);
void board_data(brd);
brd board1;
screen(500,500, 0, "I also need this");
SDL_WM_GrabInput(SDL_GRAB_ON); // For mouse focus on window
board_data(board1);
redraw();
play(board1);
redraw();
sleep();
return 0;
}
void board_data(brd board1)
{
horLine(100, 100,400, RGB_Yellow); //All quick cg functions
verLine(100, 100,400, RGB_Yellow);
horLine(400, 100,400, RGB_Yellow);
verLine(400, 100,400, RGB_Yellow);
horLine(200, 100,400, RGB_Yellow);
horLine(300, 100,400, RGB_Yellow);
verLine(200, 100,400, RGB_Yellow);
verLine(300, 100,400, RGB_Yellow);
board1.crd[0].x1=100;
board1.crd[0].y1=100;
board1.crd[0].x2=200;
board1.crd[0].y2=200;
}
void play(brd board1)
{
print("Foolish");
while(SDL_PollEvent(&event))
{
if(event.type == SDL_MOUSEBUTTONDOWN)
SDL_WM_SetCaption("Mouse isdown.",0);
}
}
The program prints foolish on the screen but no change for the WIndows Title or like that
What should I do.?
OneSadCookie
2006.09.29, 07:54 AM
You do need to do things like SDL_Init...
Find a simple SDL example on the 'net. There are hundreds. Copy it.
admirer
2006.09.30, 09:53 AM
Hello ,I said im using Quick CG library .So i neednot bother about it all like SDL init like that..
Now I have developed the tic tac toe frame work.Now im possible to draw dot and cross across the screen in the user input box..
I have some more doubts...
1. How to get user i/p in this mode
2. What to do with SDL threads.. As a beginner
3. How can i make another screen then hide it or destroy it to make another screen like that.
4. Is there any good tutorials in the web that describes alpha beta pruning
5. Is SDL better than OpenGL for simple graphics?
6. How can I port from windows ->Linux using sdl... (asking about sdl.dll )
********** Thank You Taxxodium for your valuable help ********************************
( I was missing the while loop)
************************************************** ************************************************
OneSadCookie
2006.09.30, 06:36 PM
1) you can't reliably get a useful IP for the user, you shouldn't ever need it, and you shouldn't try.
2) nothing. If you're a beginner, stay the heck away from threads.
3) SDL can only have one window at once.
4) Yes. http://google.com/
5) OpenGL will always be faster than SDL's 2D stuff. I also think it's easier, though that's more controversial.
6) If your code is cross-platform, it will "just compile" on Linux.
admirer
2006.10.01, 10:41 AM
Sad Cookie,
Why you said as I am beginner i should keep away from threads?
Any way, I need to ask one more question?
1. I want to display something in the console window with the application
I tried compiling the whole project as a console window application
It displayed the console window and SDL window
But whatever i wrote inside printf wasnt displayed .I tried with fprintf and all .Nothing succeeded.
What should I do?
Taxxodium
2006.10.01, 12:28 PM
Why you said as I am beginner i should keep away from threads?
Because threading belongs in the advanced programming category. You should only attempt threading if you have a full understanding of how memory management goes, how data that is used in multiple threads can get corrupted and what you should do in order to prevent this.
Most of the time you don't need threads anyway. So, stay out of them.
I want to display something in the console window with the application
I tried compiling the whole project as a console window application
It displayed the console window and SDL window
But whatever i wrote inside printf wasnt displayed .I tried with fprintf and all .Nothing succeeded.
What should I do?
You you understand which functions you need to use. fprintf is only used when writing to file, hence the first f in the function. printf should work, unless you're code looks bad. If you're using C++, you can use cout which is more flexible than printf, though I don't really use it.
Examples:
in C:
printf("This is a test\n");
in C++:
cout << "This is a test" << "\n";
OneSadCookie
2006.10.01, 05:19 PM
in C++:
cout << "This is a test" << "\n";
Should read
std::cout << "This is a test" << std::endl;
Printf is nicer :p
PowerMacX
2006.10.02, 09:22 AM
Should read
std::cout << "This is a test" << std::endl;
Printf is nicer :p
Yes, whoever thought that overloading the bitshift operator for input-output in C++ was a good idea has a strange sense of humor... :mad:
admirer
2006.10.13, 12:32 PM
Rule Value
1. Human has 2 marks in a row, not blocked - possible victory. -9
2. Computer has 2 marks in a row, not blocked - possible victory. +5
3. Computer mark beside, above, or below last played human mark (no diagonal). +3
4. Computer mark yields "far advantage" - 2 marks on opposite sides giving possible victory condition. additional +1 including the +5 for possible victory.
5. Computer has 3 marks in a row - win! +30
I am not able to understand the fourth rule
Can anyone help me regarding this?
sohta
2006.10.13, 07:41 PM
I am not sure exactly what that rule means. However, I have to wonder, why do you need such rules? Considering Tic-Tac-Toe has such a small phase-space, you could simply use the Minimax algorithm and be done with it. You do not need any heuristic function beyond:
Win: 1
Lose -1
draw 0
Or am I missing something here?
kelvin
2006.10.13, 09:42 PM
Yes, whoever thought that overloading the bitshift operator for input-output in C++ was a good idea has a strange sense of humor... :mad:
Actually, it makes sense from a language designers perspective.
When considering operators to overload you must consider:
precedence
associativity
For the stream catenation you want low precedence and left-hand associativity (both of which are properties of the bit-shift op).
Consider:
x = 1 << 1 + 1 << 3; // ((1 << (1 + 1)) << 3) = 32
// NOT "(1 << 1) + (1 << 3) = 10". see? wrong!
std::cout << 1 + 2 << 2; //prints "32"
//i.e. (1+2) gets appended to cout which evaluates to a stream, lets call A
// A << 2 appends "2" to the the result of the previous stream append.
In the stream catenation we want all other operations to happen before we print the result (hence the need for low precedence). We want this because it reduces type conflicts. Lets say we evaluated the stream cat in my above example first. We'd then have the result of "cout << 1" (a stream) and " + 2"(an int) which is not defined in this context.
We need the left associativity because evaluating the right side terms first again leads to type problems. In my example, if we evaluate "1 + 2 << 2" first, we get 12 (it'd be worse if we had another type, say string, here instead) instead of appending "2" to the stream.
So when it came time to design C++ these were the major factors in choosing an operator to overload. The bit-shift op is one of the few in the C language that fits with these two properties. The fact that it looks like an arrow was a convenient coincidence.
OneSadCookie
2006.10.13, 11:03 PM
Or, they could not have randomly overloaded any operators with absurdly different meanings, and instead you could say
std::cout.put(1 + 2).put(2)
or, heaven forbid,
printf("%d%d", 1 + 2, 2);
:p
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.