How to handle a mouse click?

Pages: 12
How can I do something when the mouse is clicked? Believe me, I've Googled this thing so much, but all I see it how to simulate a mouse click, not how to handle on. Say I want to click somewhere on the screen. It doesn't matter where. When the user clicks their mouse (left mouse button) it prints "You clicked your left mouse button!". I'm going to use this for my gun shooting thing.

Thanks, sorry if this is a noobish question. I understand that C++ isn't an event driven programming language?

Thanks,

-PG


***THIS QUESTION WAS EDITED FROM SOMETHING HARD TO SOMETHING EASY. SORRY IF THE REPLIES MAKE NO SENSE :)***
Last edited on
You must to learn to walk before you run.

http://www.cplusplus.com/doc/tutorial/
Yes, I know that, but I prefer to run before I can walk ;)

Is the code above correct? Thanks,

-PG
No. Not by a long shot.
Well, that's helpful. Thank you for your descriptive and helpful answers!

Maybe I'd have more luck with someone else...


I didn't ask this question to be slammed. Let's pretend that I said I'm a professional, I've been coding since I was 5 years old, blah blah blah.

Nevermind. Topic 'solved'.
Last edited on
He didn't slam you...
You asked him a question and he answered it...
I'm sorry, :), but I don't count "It's impossible! No, your code is all wrong!" as an answer. Sorry :)

Why do You define your functions inside of main?... You will not be able to call them from the "main-outside"...
1
2
3
4
5
6
7
int OnMouseClick ()
  {
    int MouseClicks1 = 0;
    int MouseClicks2 = MouseClicks1 +1;
    int MouseClicks3 = 30 - MouseClicks2;
    PlaySound("sounds/gunshot.wav");
  }


when running this function your´ variables become created over and over again (same to the initializiation)... so it will always be 1 shot made and 29 left...
To hold the values of the Shots left and shots made u just need one variable (maby an additional constant to define how big ur magazine is)...

you really better start off with the tutorial posted above:)...
This is probably worse than the people coming in here wanting to create games like WOW. One plus to this thread is that he used proper english. However, this (plus) is cancelled out by the fact that he doesnt accept help from someone who obviously has more experience than he does. Especially since he had trouble with Hello world.
Look, guy, if you ask questions, you have to be ready for the answers.
On my side, lying and telling the truth are exactly the same. I'm not going to be using my answers. I could have said "yeah, it looks great and your grasp of the logic of C++ is accurate. I estimate you'll have a fully working physics engine in a week" and it wouldn't have made a difference to me. However, anything other than the truth wouldn't have really helped you. And the truth is, when I look at the code you posted, I see code written by someone who, while they seem to understand the basics of procedural programming, is making the fatal mistake of assuming a language has features that haven't been mentioned, and in fact doesn't have.
This is also the code of someone who (although I hate to repeat myself) is trying to run before they can walk, and that's something you just can't do in programming. I would never have even considered that this code was written by someone with more than a year of programming. An "older" programmer would know this already, probably by experience.

So, in summary, this is what I gather from everything you've posted:
1. You're not ready to write a physics engine, or anything even remotely as complex. At least not in C/++.
2. You won't be ready for at least six months, or until you can finish a basic C++ (or C, but simulation can benefit from OOP) tutorial, whichever comes last.

And this is my advice, in order of decreasing importance:
1. Shut up and listen.
2. Related to the previous point, an answer isn't falsified by you not agreeing with it. Being prepared to accept the answer is a requirement when asking a question.
3. Don't skip steps when learning. It can only lead to disaster. "This is the steering wheel, this is the gas pedal, and to save time we'll skip the brakes. You now know how to drive!"
4. Never assume features about a language. I can't imagine what gave you the idea that C++ supports event-driven programming. (Note: That sentence doesn't imply event-driven programming is impossible in C/++.)
Last edited on
I've been a student for over 2 years and have ton of software engineering and a little bit of C++ and C# under my belt. Two people I would always take advice from (people that know their stuff) are helios and disch (not saying anyone else is irrelevant); of course I've only been a member here since the beginning of 2010. C++ IS an object oriented programming (OOP) language, if you say member.whatever; it will do exactly what you say. And any variable declared before or inside of main WILL be global variables; accessible from anywhere in the program. Unfortunately, I haven't the slightest idea what you are trying to accomplish with your code, aside from counting down mouse clicks representing bullets fired.

P.S. If people seem to answer somewhat rudely, you might want to present more information in your first post. :) Also, do as much research as you can before posting.
whoah! that's bad.. : (
And any variable declared before or inside of main WILL be global variables;

Not quite, they'll just be local variables.
Definition: global variable: (straight from college textbook) "global variables are static variables and by default, variables declared within a block are automatic variables. Any variable declared in a function main is accesible anywhere in the program. " So, in essence, local variables in main can be considered global, just like variables declared before the entry point.

D.S. Malik - C++ Programming
Last edited on
Any variable declared in a function main is accesible anywhere in the program.
That last bit is simply not true, and I can demonstrate it quite easily.
1
2
3
4
5
6
7
8
void f(){
     a++;
}

int main(){
    int a=10;
    f();
}

That's a pretty big mistake to make.
Last edited on
I'm sorry, :), but I don't count "It's impossible! No, your code is all wrong!" as an answer.

That's not what he said. He said what you wrote was incorrect.

A physics engine is a complex thing. Are you trying to outdo (for example) the Euphoria engine in GTA IV? Or the CryEngine 3? Because you won't have alot of luck there. These are game engines made by teams of ten or more excellent programmers.

At the same time, keep going and good luck. And don't take any notice of helios' attitude. He has a tendency to sound mean; really he's just your friendly neighbourhood cynic.

And any variable declared before or inside of main WILL be global variables

Find a new book.
im wanting to make a 3d physics engine thingie...
task 1:Create a 3d rendering header program thingie. >.>
no, how do i create a dialog box with windows? -.-
@chrisname
yeah, sometimes i also feel like helios sound offensive and insulting but i always think maybe he didn't mean it or he just didn't find the right word to say..

anyway i think he's good programmer ^ ^

IMO, sometimes were having cultural difference.. you know the way we interpret things..
No, I'm just ill-mannered.
Last edited on
@helios
i don't think you're ill-mannered? if so, how do you work with others then?

and WTF is with the global variable thing, can anyone tell the theory or something.
global variables are static variables and by default
is this true!
Pages: 12