Hi everyone. I’m new to SDL.
Hi Tom.
My goal is to display a .jpg image on the screen. If I click the
image, it
diplays a new image. Yeah, it doesn’t do much, but I thought it was a
good
first project.
I wrote something, and it works (ignoring the glaring memory leak).
But I
really don’t know all that the SDL API offers and am having a tough
time
gleaning it from the information online. So, here’s the code. If
someone
can suggest methods or techniques to improve on it I would really
appreciate
it.
First I’ll make some remarks on your code, then I’ll show you a revised
version. I don’t mean to sound rude, but you should consider learning
more about programming before learning about using SDL. But that’s just
my personal opinion, take it or leave it.
~~~~~~~*
a header file kiosk.h
include <SDL/SDL_image.h>
#include <SDL/SDL.h>
using namespace std;
Do you know what this line of code does?
class Kiosk
Classes are cute, but an unnecessary level of organization for such a
simple program (though I don’t know how much more you plan on adding to
it. If you plan on adding much more, this was a wise decision.)
{
public:
SDL_Surface * screen;
Because your screen pointer and various initialization code are located
within the Kiosk class, one might think that your Kiosk class really
represents your whole application. In that case however, all the code
that controls the flow of your application that is located in the
main() routine, should probably instead be located in the Kiosk class.
Search online or buy a good book about object oriented programming (or
OOP.)
SDL_Rect pictureLocation;
SDL_Surface * image;
Kiosk()
{
screen = SDL_SetVideoMode(1024, 768, 16, SDL_DOUBLEBUF);
}
You should check for failures, and you did not initialize SDL. You also
seem to have a syntax error here, a semi colon must follow the
declaration of a class member function.
void showPicture(char * pic)
{
if(image != NULL){
image = NULL;
}
Why do you do this? Firstly, it would be faster, and have the exact
same effect, to simply say “image = NULL.” Secondly, the next thing you
do below is assign image to the return value of a function, therefore
losing the NULL assignment you just made. Therefore, erasing the
contents of the image pointer is quite useless.
I could have just said that there is no functional reason for assigning
NULL to image, but I wanted to be very explicit so that you will learn
a little more.
image = IMG_Load(pic);
Again, you should check for failure.
pictureLocation.x = 100;
pictureLocation.y = 0;
You never assign anything different than 100 and 0, why don’t you do
this during initialization instead? Perhaps in the future you could
center the image here instead of always placing its in the same spot.
(Or maybe the images are all the same size and they ARE all centered
and I just have no way of knowing…)
SDL_BlitSurface(image, NULL, screen, &pictureLocation);
SDL_Flip(screen);
Since you didn’t clear the screen contents, I assume here that your
images are all the same size, because otherwise you’d see their edges
sticking out all over the place.
Actually what I just said is partially incorrect. Because you see, you
only have two pictures, and since there are only two buffers being used
by SDL_Flip(), the same picture always gets drawn to the same screen
buffer, so you in fact won’t see edges of past pictures sticking out
until you both use different sized images, and use more than 2 images.
}
Once again, syntax error.
};
Next file:
#include “kiosk.h”
using namespace std;
Again, are you sure you know what this does? I recommend buying a book
about C++.
SDL_Event event;
int change = 0;
“change” is probably not a very descriptive name for this variable to
most people, but perhaps you think so, and since it’s a matter of
opinion, that’s entirely up to you.
And I’ll repeat a previous comment, if your Kiosk class represents your
whole application, you should consider moving things like this into the
kiosk class.
int main(int argcs, char * pArgs[])
{
Kiosk k;
k.showPicture("…/Pictures/k_photos/green.jpg");
If you insist on using the Kiosk class, a more object oriented approach
might be for the kiosk class to show a picture automatically, if its
only function is to show pictures, it seems like a good idea to me.
while(1){
while(SDL_PollEvent(&event)){
switch(event.type){
case SDL_MOUSEBUTTONDOWN:
// mouse position is event.button.x, event.button.y
if(event.button.button == SDL_BUTTON_LEFT){
if(change == 0){
k.showPicture("../Pictures/k_photos/face.jpg");
change = 1;
}
else{
k.showPicture("../Pictures/k_photos/green.jpg");
change = 0;
}
In general this code here is not very “scalable.” What that means is
that it’s not looking to the future, and in this case, it’s very
specifically written for two pictures. Not only that, but good
programming practices would allow you to write code for an arbitrary
number of pictures just as easily as for two pictures. In my revision
of your code you can see how.
}
else{
// right click
return 0;
There are other things you must do before ending your program. However
you should consider putting them in the destructor of your Kiosk class
if you still choose to use such a class. You should also consider
catching other events in order to quit. Like when the window is closed
by the user.
}
default:
// nothing
break;
Adding default: break; to your code is unnecessary. Your C or C++
program will operate the exact same way whether or not you add this
code.
}
}
}
return 0;
Why do you return here? Your program will never get to this part
because it is in a while(1) loop.
}
Now you can read the revised version. It is attached. Here are some
notes:
- I chose not to use the kiosk class.
- My code is prepared for more filenames to be added to the source
code. Try revising this code so that it will take a list of filenames
from the command line!
- I did some things in unconventional or strange ways to demonstrate
what you CAN do, not necessarily what you SHOULD do. If you read the
comments you’ll see which are which.
- I didn’t take my own advice and have it load a picture right away,
you do that yourself.
- Since neither of our programs even use the picture other than to
blit it once, you may want to consider unloading it from memory as soon
as you’re done blitting it!
ps. I’ve never attached a file using Mac OS X’s Mail.app before, I hope
I did it right!
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed…
Name: someguy.cpp
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20040725/d0de13eb/attachment.txtOn Jul 25, 2004, at 11:48 AM, Tom Lynch wrote: