[SDL] Blitting Image With Mouse Click

Jared Maddox absinthdraco at gmail.com
Sun Mar 25 17:27:54 PDT 2012


I can't believe that I forgot to read the other digests before sending
that last message...

> Date: Sat, 24 Mar 2012 21:33:37 -0700
> From: "GameCoder" <g_andy at live.com>
> To: sdl at lists.libsdl.org
> Subject: Re: [SDL] Blitting Image With Mouse Click
> Message-ID: <1332650017.m2f.32370 at forums.libsdl.org>
> Content-Type: text/plain; charset="iso-8859-1"
>

> What I want to do is, is so that when
> the user clicks somewhere within the screen bounds, a tower will be drawn
> onto the location of the screen, while also subtracting money from the
> player.


> stack <int> Towers;
> int gtowers;

What is this? What does this do? You have a stack of integers, which
is named Towers, and you have an int named gTowers, and no real
indication of what either is for. Just for reference, if you call
size() on Towers, it should return the number of ints that it
currently has inside itself.


> Tower gTower();

In C++, Tower gTower() means that gTower returns a Tower instance, and
DOES NOT take any arguments.


> int TowerHealth = 100;

TowerHealth? That means that towers can be damaged, correct? Unless
ALL of the towers are supposed to just explode when their group-health
drops to zero, this should be a variable that each tower has it's own
version of.


> int Points = 0;
>
> int Money = 1000;

Ok, so you have a global variable that records how much money the
player can spend, and another that records how many points the player
has earned, that works. However, if you decide to make this a
two-player game in the future, then you'll need to change it.


> int EnemyHealth = 50;

That thing about TowerHealth? If you're going to have more than one
enemy on the screen at once, then it applies to EnemyHealth too.


>             if(event.type == SDL_MOUSEBUTTONDOWN)
>             {
>                 if(event.button.button == SDL_BUTTON_LEFT)
>                 {
>                     mx = event.button.x;
>                     my = event.button.y;
>
>                     if(mx < 0 && mx < WIDTH)
>                     {
>                         gTower(gTower.Draw());
>                     }

This seems to be the only time in the entire game that you display a
tower. The problem is, the
SDL_FillRect(Screen,NULL,SDL_MapRGB(Screen->format,0,0,0)) below will
erase the image of the tower. As a result, the tower will only display
for 1 single frame, and then never again.

Also, what is gTower(gTower.Draw()) supposed to do? I can tell you
what it actually does: it calls gTower(), calls Draw() on the return
value of that gTower call, then uses Draw's return value as an
argument to gTower(). Unfortunately, this clashes with the definition
of gTower, which does not take any arguments. Were you attempting
operator chaining?

Incidentally, I would subtract the money here, in the SDL_BUTTON_LEFT
handler, instead of making the Tower class deal with it. Two reasons:
1) That restricts all uses of the Money variable to the same file
where it's declared. This is a good idea.
2) If the player doesn't have enough money, then you can refuse to
build a tower without calling the tower's constructor. Also a good
idea.


>         SDL_BlitSurface(Backdrop,NULL,Screen,&BackdropRect);
>         SDL_BlitSurface(FPS,NULL,Screen,&FPSRect);
>         SDL_BlitSurface(POINT,NULL,Screen,&POINTRect);
>         SDL_BlitSurface(HEALTH,NULL,Screen,&HealthRect);
>         SDL_BlitSurface(MONEY,NULL,Screen,&MoneyRect);
>

Once again, you should draw all of the towers after drawing the
Backdrop, but PROBABLY before drawing the FPS.


> Date: Sun, 25 Mar 2012 09:17:34 -0700
> From: "GameCoder" <g_andy at live.com>
> To: sdl at lists.libsdl.org
> Subject: Re: [SDL] Blitting Image With Mouse Click
> Message-ID: <1332692254.m2f.32374 at forums.libsdl.org>
> Content-Type: text/plain; charset="iso-8859-1"
>
> I see. OOP is something I don't something fully understand. Just seems like
> more code for better organization, organization is fine for large projects
> and all, but this was meant to be a small project. Its the OOP I'm having
> trouble with, with the Tower class and stuff.

I once read in a C book (I think it might have been the K&R book,
actually) that a function is just a small program.

And thinking about it, that's right.

And thinking about it, an object is the exact same thing (just
implemented differently).

Basically, objects are 0 or more pieces of data and 0 or more
functions that act like a single, individual THING. Hence why they're
called objects: objects are things & vice-versa. The basic idea is
that you take something that can be considered a THING (such as your
towers, or a video card, or a mailing list, or whatever else), and you
design a GROUPING of data & functions that behaves like that THING
behaves.


> Like how do I use the tower
> class in the main game code?
>

I think you're approaching it from the wrong perspective. 'How do I
use this?' comes SECOND, first you need to know what a tower is. A
tower is, at the very minimum, a THING that has a POSITION (expressed
as a X position & a Y position), and an APPEARANCE (which is provided
by a SDL_Surface).

Now, do all towers look the same? If so, then you only need one
SDL_Surface for all of them. Otherwise, you'll need more than one
surface.

Can towers be damaged? Do they get damaged as a group (like Star
Trek's exploding control consoles), or individually? If they get
damaged individually, then they all need their own health variable.

Keep asking yourself questions about towers, until you completely know
what a tower is, then put all of the appropriate variables into your
Tower class. Don't go insane with this; if you aren't certain (or even
currently trying) to USE a particular trait that towers have, then
don't add it. You might want to leave /* ADD THIS */ notes to yourself
about certain tower traits, but it's easier to get something small
working than something big.

After you have all of the variables placed into the Tower class, THEN
you ask yourself "How do I use this?". The answers to THAT will tell
you two things:
1) What member-functions to add to the class, and
2) What extra variables you need to add into the class for those
member-functions to work.

For example, you'll need to draw the tower, so you'll need a draw
function, which will need some way to figure out WHERE to draw to. If
towers have health you'll need a function to modify their health. Etc.



More information about the SDL mailing list