using event handler - c++

is there a way to use mouse as an event handler in c/c++ im making a game on snakes and ladder (the famous board game) and trying to make it with basic borland c++ compiler working with a header file called graphics.h, which is very basic and gives output of 640 X 480 res, so I was wondering if there is a possiblity of using mouse as an event handler (about which i have no experiance)to have control over the palyer coins on the board.

I'm not sure which version of graphics.h you happen to have, but there are functions getmousey, getmousey, clearmouseclick and getmouseclick. See this for some documentation that may work for you.
It appeas that you can use registermousehandler and a call-back function to do some level of event-based programming. Here's a sample from the document I sent you.
// The click_handler will be called whenever the left mouse button is
// clicked. It checks copies the x,y coordinates of the click to
// see if the click was on a red pixel. If so, then the boolean
// variable red_clicked is set to true. Note that in general
// all handlers should be quick. If they need to do more than a little
// work, they should set a variable that will trigger the work going,
// and then return.
bool red_clicked = false;
void click_handler(int x, int y)
{
if (getpixel(x,y) == RED)
red_clicked = true;
}
// Call this function to draw an isosoles triangle with the given base and
// height. The triangle will be drawn just above the botton of the screen.
void triangle(int base, int height)
{
int maxx = getmaxx( );
int maxy = getmaxy( );
line(maxx/2 - base/2, maxy - 10, maxx/2 + base/2, maxy - 10);
line(maxx/2 - base/2, maxy - 10, maxx/2, maxy - 10 - height);
line(maxx/2 + base/2, maxy - 10, maxx/2, maxy - 10 - height);
}
void main(void)
{
int maxx, maxy; // Maximum x and y pixel coordinates
int divisor; // Divisor for the length of a triangle side
// Put the machine into graphics mode and get the maximum coordinates:
initwindow(450, 300);
maxx = getmaxx( );
maxy = getmaxy( );
// Register the function that handles a left mouse click
registermousehandler(WM_LBUTTONDOWN, click_handler);
// Draw a white circle with red inside and a radius of 50 pixels:
setfillstyle(SOLID_FILL, RED);
setcolor(WHITE);
fillellipse(maxx/2, maxy/2, 50, 50);
// Print a message and wait for a red pixel to be double clicked:
settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
outtextxy(20, 20, "Left click in RED to end.");
setcolor(BLUE);
red_clicked = false;
divisor = 2;
while (!red_clicked)
{
triangle(maxx/divisor, maxy/divisor);
delay(500);
divisor++;
}
cout << "The mouse was clicked at: ";
cout << "x=" << mousex( );
cout << " y=" << mousey( ) << endl;
// Switch back to text mode:
closegraph( );
}

You can use Mouse and Keyboard events using Win32 Programming (In Visual Studio).
Is it the requirement to use borland c++.
I think similar APIs are there in borland c++.
You can refer http://www.functionx.com/win32/index.htm for more information on event handling in Visual Studio using Win32 Programming.

Related

my if condition is not working on processing language

I'm working on Processing language for my assignment. It's an animation. The animation object(a ball) needs to go from top to bottom. I have declared variable as float x,y. Whenever I put if condition to increase size of it by 1 but it's not moving a single inch.
float x;
float y;
size(600, 400)
x = 0.4*width/8;
y = 0.4*height/8;
ellipse( width/2, x, 0.8*width/8, 0.8*width/8);
ellipse( y, height/2, 0.8*height/8, 0.8*height/8);
if(x < height){
x = x+1;
}
if(y < width){
y=y+1;
}
I am expecting the output as- the ball located on top moves towards down and stops at the bottom and the left ball moves to right and stops at the rightmost point.
You're using Processing in "static mode" which means your code runs once and then is done. Nothing happens after you reach the end of your code.
To take advantage of Processing's 60 FPS rendering loop, you need to specify setup() and draw() functions. Something like this:
float circleY;
void setup(){
size(200, 200);
circleY = height/2;
}
void draw(){
background(200);
ellipse(100, circleY, 20, 20);
circleY = circleY + 1;
}
Shameless self-promotion: here is a tutorial on animation in Processing.

can't change location of ship, function call not working

This is the source file where the function setLocation() is called (that includes for the graphics header file should have angle brackets, but it disappeared, so I used quotes)
#include "SFML/Graphics.hpp"
#include "ship.h"
const int WINDOW_WIDTH = 500;
const int WINDOW_HEIGHT = 500;
//==============================================================================
int main()
{
sf::RenderWindow window( sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT),
"Delta Quadrant", sf::Style::Titlebar | sf::Style::Close);
window.setFramerateLimit(120);
// this causes loop to execute 120 times a second at most.
// (a delay is automatically added after screen is drawn)
Ship obj;
//ADD Code to set limits on ships location (call setMaxLocation);
//sets position of the ship in the middle of the screen
obj.setLocation(250, 250);
while (window.isOpen())
{
//----------------------------------------------------------
//handle user input (events and keyboard keys being pressed)
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
//turn left with press of left button
while (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
obj.rotateLeft();
//turn right with press of right button
while (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
obj.rotateRight();
//apply thrust with press of up button
while (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
obj.applyThrust();
//----------------------------------------------------------
//draw new frame
window.clear();
//draw ship
obj.updateLocation();
obj.draw(window);
//redisplay window
window.display();
}
return 0;
}
This is setLocation()'s definition, in the ship source file (same issue with the angle brackets, used quotes again).
#include"cmath"
#include "SFML/Window.hpp"
#include "SFML/Graphics.hpp"
#include "vector.h"
#include "ship.h"
//Constants
const double PI = 3.14159;
const double THRUST = 0.005;
const double TURN_SPEED = 1;
//constructor
Ship::Ship(){
maxLocations.x = 500;
maxLocations.y = 500;
radius = 5;
location.x = 0;
location.y = 0;
velocity.x = 0;
velocity.y = 0;
angleDeg = 5;
}
void Ship::setLocation(double x, double y){
//Check and correct for the ship going out of bounds.
if (x < 0)
location.x = 0;
else if (x > maxLocations.x)
location.x -= maxLocations.x;
else
location.x = x;
if (y < 0)
location.y = 0;
else if (y > maxLocations.y)
location.y -= maxLocations.y;
else
location.y = y;
}
void Ship::updateLocation(){
location.x += velocity.x;
location.y -= velocity.y;
//Check and correct for the ship going out of bounds.
if (location.x < 0)
location.x = 0;
else if (location.x > maxLocations.x)
location.x -= maxLocations.x;
if (location.y < 0)
location.y = 0;
else if (location.y > maxLocations.y)
location.y -= maxLocations.y;
}
void Ship::draw(sf::RenderWindow& win) {
//Initializes the Ship class to an object
Ship obj;
// draw ship
sf::ConvexShape ship;
ship.setPointCount(3);
ship.setPoint(0, sf::Vector2f(10, 0));
ship.setPoint(1, sf::Vector2f(0, 25));
ship.setPoint(2, sf::Vector2f(20, 25));
sf::Vector2f midpoint(10,15);
ship.setOrigin(midpoint);
ship.setFillColor(sf::Color(0, 0, 0));
ship.setOutlineThickness(1);
ship.setOutlineColor(sf::Color(255, 255, 255));
ship.setPosition(obj.getLocation().x, obj.getLocation().y);
obj.setAngle(obj.getAngle());
win.draw(ship);
}
}
Finally this is the header file where the setLocation() prototype is located
#ifndef SHIP_H
#define SHIP_H
#include "vector.h"
class Ship {
private:
Vector maxLocations; //maximum allowable values for location
Vector location; //current location (x,y)
Vector velocity; //current velocity (in pixels/frame)
double angleDeg; //angle ship is facing, in degrees
double radius; //gross radius of ship (for collision detection)
public:
//constructor
Ship();
//=============================================
//mutators
void setLocation(double x, double y);
void updateLocation();
void draw(sf::RenderWindow& win);
...
};
#endif
My issue is when I call setLocation() it doesn't change the location vector (as defined in the second source file) from (0, 0). When compiled the ship remains at (0, 0). What I want to happen is when I call setLocation(), the ship needs to change from (0, 0) to (250, 250), thus the ship on compiling starts in the middle of the screen, not a corner.
The problem at hand is you are not using the Ship object that you believe you are using. What I mean by that is you aren't drawing the ship object in your main function, instead you are creating a temporary ship object in your Ship::draw() method and then using that ships position to draw your shape. So the position will always be (0, 0) because your temp ship object get's initialized every frame with the position (0, 0) and then you use that temporary ship's position to draw your shape.
To make this more clear let's look at your code.
void Ship::draw(sf::RenderWindow& win) {
// This is a temporary object and is not the same as the one you defined in your
// main.cpp file. When this object is constructed it's location is (0, 0) by
// default.
Ship obj;
// draw ship
sf::ConvexShape ship;
ship.setPointCount(3);
ship.setPoint(0, sf::Vector2f(10, 0));
ship.setPoint(1, sf::Vector2f(0, 25));
ship.setPoint(2, sf::Vector2f(20, 25));
sf::Vector2f midpoint(10,15);
ship.setOrigin(midpoint);
ship.setFillColor(sf::Color(0, 0, 0));
ship.setOutlineThickness(1);
ship.setOutlineColor(sf::Color(255, 255, 255));
// Here we are setting the shapes position to the position of the temporary
// Ship object that you defined above. Remember that it's default location is
// (0, 0) when constructed and you never changed it's location in the draw
// method.
ship.setPosition(obj.getLocation().x, obj.getLocation().y);
obj.setAngle(obj.getAngle());
win.draw(ship);
}
Now how do you fix this? That is easy. All you need to do is get rid of that temporary ship object in your draw method and instead use your location vector to set the position of the shape. Like this.
ship.setPosition(location.x, location.y);
It might be beneficial for your to study up on how objects and classes work in C++ before continuing on. It seems you might not understand exactly how they work and how different scopes of code work. But I will leave that for your to decide what to do.
Though I would like to point out a few other pointers for you.
1 - In your draw method you are updating the position of objects. This is generally a bad thing to do and you should avoid it. All updating code like when you change a objects position should happen in a update function/method. The drawing methods of entities should only have drawing code in them. The reason for this is because usually drawing and updating happen at different time steps in the main game loop.
2 - Your updateLocation method won't work like you think it will. The reason is because you are doing location.y -= velocity.y; when in reality you should be adding the velocity of y to location just like you did with x.
3 - I would advise against using sfml's setFrameRateLimit() method and instead use your own timestep which will be much more accurate (SFML's version uses sf::Sleep which is notorious for being inaccurate) and give you much more control over your time step. If you don't know about time steps I would highly suggest reading this great article on it. It will save you hours of headaches. http://gafferongames.com/game-physics/fix-your-timestep/
4 - Let's take a look at this line.
else if (x > maxLocations.x)
location.x -= maxLocations.x;
Now let's say that maxLocations.x = 500 like in your example and let's also say that location.x = 550. Do you really want the location to be x = 50? Wouldn't a more logical step be to set x to x = maxLocations.x? Not sure if this is what you wanted but thought I would point it out.
Hopefully this will help you a bit and I would always be glad to answer any other questions you might have. Wish you the best of luck with your project.

C++ how to drag bitmap around screen

Hi so I'm trying to make it so a little UFO bitmap (drawing/painting already taken care of) can be dragged around the screen. I can't seem to make the UFO position update and then redraw repeatedly from the MouseButtonDown() function (simplified code for mouse event handler). Any suggestions on detecting the dragging and redrawing accordingly? Code is below for relevant functions:
void MouseButtonDown(int x, int y, BOOL bLeft)
{
if (bLeft)
{
while(_bMouseMoving == true && _bMouseDragRelease == false) {
_iSaucerX = x - (_pSaucer->GetWidth() / 2);
_iSaucerY = y - (_pSaucer->GetHeight() / 2);
InvalidateRect(_pGame->GetWindow(), NULL, FALSE);
}
// Set the saucer position to the mouse position
_iSaucerX = x - (_pSaucer->GetWidth() / 2);
_iSaucerY = y - (_pSaucer->GetHeight() / 2);
}
else
{
// Stop the saucer
_iSpeedX = 0;
_iSpeedY = 0;
}
}
void MouseButtonUp(int x, int y, BOOL bLeft)
{
_bMouseDragRelease = true;
}
void MouseMove(int x, int y)
{
_bMouseMoving = true;
}
To clarify what chris said, you're only going to get the WM_xBUTTONDOWN message once, and you'll need to use that to toggle a dragging state that you can query when you recieve a WM_MOUSEMOVE message.
When you get the mouse move message during a dragging state, you'll want to invalidate the rectangle surrounding where the ufo was, and the rectangle surrounding where it is.
Invalidating a rectangle causes WM_PAINT messages, where you redraw whatever was behind the ufo, and the ufo in it's new place.
Or you could cheat and make the UFO a cursor when you're dragging :)

Trapping the mouse?

I'm using GLUT and developing a FPS game. I need a way to trap the mouse so that the camera continues to move because right now when the mouse position exceeds the monitor limit, there is no way to calculate change in X or change in Y. How can I 'trap' the mouse with GLUT?
Thanks
I'd recommend using a ready-made engine like OGRE 3D instead, but if you really want to reinvent the wheel, here's how...
In all cases I'm aware of, PC FPS games "trap" the pointer by registering a mouse motion callback, noting the relative motion, and then warping the pointer back to the center of the window.
Here's some code I wrote to add mouse input to a sample ping-pong table in an OpenGL with C++ course a year or two ago:
void resetPointer() {
glutWarpPointer(TABLE_X/2, TABLE_Y/2);
lastMousePos = TABLE_Y/2;
}
void mouseFunc(int sx, int sy) {
if (!started) { return; }
int vertMotion = lastMousePos - sy;
lastMousePos = sy;
player1.move(vertMotion);
// Keep the pointer from leaving the window.
if (fabs(TABLE_X/2 - sx) > 25 || fabs(TABLE_Y/2 - sy) > 25) {
resetPointer();
}
}
// This goes in with your "start new game" code if you want a menu
resetPointer();
glutSetCursor(GLUT_CURSOR_NONE);
glutPassiveMotionFunc(mouseFunc);
It only tracks vertical motion, but adding horizontal is trivial.

rectangle disappearing with SDL

So I'm simply trying to make a red 10 x 10 box move vertically back and forth. I compile and run my program and the red box appears starts moving down, then just disappears after it hits the edge of the screen. I used some cout << statements that tell me when the functions are being called and they are all being called when they are supposed to. Even when the box can't be seen the functions are properly being called.
My main loop
while(running)
{
myScreen->Clear();
boxes.Move();
boxes.Draw();
myScreen->Flip();
........
My draw() function
SDL_Color red;
red.r = 255;
red.g = 0;
red.b = 0;
if( SDL_FillRect( my_screen->Get_screen(), &start_dest, SDL_MapRGB(
my_screen->Get_pixel_format(), red.r, red.g, red.b ) ) == -1 )`
cout << "Fill rect in Draw(); failed\n";
My Move() function
start_dest.y += y_step;
if ( start_dest.y >= my_screen->Get_height() )
{
cout << "start_dest.y >= screen height\n";
start_dest.y = my_screen->Get_height();
y_step = -y_step;
}
if ( start_dest.y <= 0 )
{
cout << "start_dest.y <= 0\n";
start_dest.y = 0;
y_step = -y_step;
}
I have been trying to find this bug forever. just leave a comment if anyone wants to see more code. Thanks
There isn't enough information to give conclusive answer, but here's a hint.
From my experience with SDL, SDL functions can modify your Rect structure when called, especially when rect is partly off-screen. Make sure you set all its properties (x,y,width,height) before each SDL function that uses the rectangle.
(I'm assuming here that start_dest has a 'height' member, and that the screen coordinates have (0,0) in the top left corner)
I think perhaps the first 'if' statement in Move() should be
if(start_dest.y >= my_screen.Get_height - start_dest.height)
so that the rectangle will bounce when its bottom hits the bottom of the screen rather than waiting until the top of the rectangle gets there. Something to that effect, anyways.