Newby question about displaying images

Hi everyone. I’m new to SDL.

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.~~~~~~~*
a header file kiosk.h

include <SDL/SDL_image.h>
#include <SDL/SDL.h>

using namespace std;

class Kiosk
{
public:

  SDL_Surface * screen;
  SDL_Rect pictureLocation;
  SDL_Surface * image;

 Kiosk()
 {
     screen = SDL_SetVideoMode(1024, 768, 16, SDL_DOUBLEBUF);
 }


  void showPicture(char * pic)
  {

     if(image != NULL){
        image = NULL;
     }

     image = IMG_Load(pic);

     pictureLocation.x = 100;
     pictureLocation.y = 0;

     SDL_BlitSurface(image, NULL, screen, &pictureLocation);
     SDL_Flip(screen);

  }

};

~~~~*~
some C++ code

#include “kiosk.h”

using namespace std;

SDL_Event event;
int change = 0;

int main(int argcs, char * pArgs[])
{
Kiosk k;
k.showPicture("…/Pictures/k_photos/green.jpg");

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;
}
}
else{
// right click
return 0;
}
default:
// nothing
break;
}
}
}

return 0;
}

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:

Quoth Donny Viszneki , on 2004-07-25 14:21:28 -0400:

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.

This is not true if the function is also defined in the same place.

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.

It can help placate the compiler and indicate that you really don’t need
to include a case for each enum value, though (as opposed to just having
forgotten some of the cases).

Why do you return here? Your program will never get to this part
because it is in a while(1) loop.

… unless a break is added later.

while (1)
{
// I put some variables that only are needed during the loop in here!
// The static keyword means it will retain its value through each iteration of our while loop. Get a book about C++ to learn more about the static keyword, it does many things.

static will also cause the variable to retain its value across calls to
the entire function. It may work in this case since main is not likely
to be called multiple times, but AFAICT it’s not at all conventional to
use static for this.

—> Drake Wilson
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20040725/d4140626/attachment.pgp

Thank you very much for the info and example code. That was way more than I
expected. You really helped clear up a few major issues for me.

ps. I’ve never attached a file using Mac OS X’s Mail.app before, I hope
I did it right!

Got it, thanks!

-tlOn Sunday 25 July 2004 14:21, Donny Viszneki wrote:

Quoth Donny Viszneki , on 2004-07-25 14:21:28
-0400:

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.

This is not true if the function is also defined in the same place.

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.

It can help placate the compiler and indicate that you really don’t
need
to include a case for each enum value, though (as opposed to just
having
forgotten some of the cases).

Why do you return here? Your program will never get to this part
because it is in a while(1) loop.

… unless a break is added later.

while (1)
{
// I put some variables that only are needed during the loop in
here!
// The static keyword means it will retain its value through each
iteration of our while loop. Get a book about C++ to learn more about
the static keyword, it does many things.

static will also cause the variable to retain its value across calls to
the entire function. It may work in this case since main is not likely
to be called multiple times, but AFAICT it’s not at all conventional to
use static for this.

—> Drake Wilson


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdlOn Jul 25, 2004, at 2:58 PM, Drake Wilson wrote:

I think I may have hit reply and then send without making any
modifications… apologies in advance.

Drake, thanks for all your help. I actually didn’t know you didn’t need
to put the semi colon after an in-line-definition of a class member
function. Thanks!

I hope you realize though that my information was really for a
beginner, and sometimes if they’re doing things superfluously, it’s
from a lack of knowledge (for instance “using namespace std” was
completely superfluous.) Personally I hope he learns from what I showed
him and then goes on to read what you added, but some of the things I
said I said because I personally felt it was what was beneficial for a
beginner.

And actually concerning static variables I actually was being very dumb
because what I had intended to do what put another layer of braces
outside the while(1) loop and put them in there. But I lost my train of
though. d’oh!

Thanks again though.On Jul 25, 2004, at 2:58 PM, Drake Wilson wrote:

Quoth Donny Viszneki , on 2004-07-25 14:21:28
-0400:

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.

This is not true if the function is also defined in the same place.

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.

It can help placate the compiler and indicate that you really don’t
need
to include a case for each enum value, though (as opposed to just
having
forgotten some of the cases).

Why do you return here? Your program will never get to this part
because it is in a while(1) loop.

… unless a break is added later.

while (1)
{
// I put some variables that only are needed during the loop in
here!
// The static keyword means it will retain its value through each
iteration of our while loop. Get a book about C++ to learn more about
the static keyword, it does many things.

static will also cause the variable to retain its value across calls to
the entire function. It may work in this case since main is not likely
to be called multiple times, but AFAICT it’s not at all conventional to
use static for this.

—> Drake Wilson


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

  1. Make data in class private
  2. use array as list of images (e.g. char *images[]=
    {“harry.jpg”, “curly.jpg”, “moe.jpg”};

or you could load that string list from a file, or even command line
options.

In reply to your inquiry concerning becoming better at SDL: there is no
substitute for actually learning the art of graphics programming. I
recommend scouring the internet, hanging out in chat rooms such as
#SDL, and learning the SDL API in and out.

Also, an excellent technique is to take apart other people’s programs
and look at how they did things!

Good luck,

Paul

Hi everyone. I’m new to SDL.

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.

~~~~~~~*
a header file kiosk.h

include <SDL/SDL_image.h>
#include <SDL/SDL.h>

using namespace std;

class Kiosk
{
public:

  SDL_Surface * screen;
  SDL_Rect pictureLocation;
  SDL_Surface * image;

 Kiosk()
 {
     screen = SDL_SetVideoMode(1024, 768, 16, SDL_DOUBLEBUF);
 }


  void showPicture(char * pic)
  {

     if(image != NULL){
        image = NULL;
     }

     image = IMG_Load(pic);

     pictureLocation.x = 100;
     pictureLocation.y = 0;

     SDL_BlitSurface(image, NULL, screen, &pictureLocation);
     SDL_Flip(screen);

  }

};

~~~~*~
some C++ code

#include “kiosk.h”

using namespace std;

SDL_Event event;
int change = 0;

int main(int argcs, char * pArgs[])
{
Kiosk k;
k.showPicture("…/Pictures/k_photos/green.jpg");

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;
}
}
else{
// right click
return 0;
}
default:
// nothing
break;
}
}
}

return 0;
}


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

  1. Make data in class private
  2. use array as list of images (e.g. char *images[]=
    {“harry.jpg”, “curly.jpg”, “moe.jpg”};

or you could load that string list from a file, or even command line
options.

In reply to your inquiry concerning becoming better at SDL: there is no
substitute for actually learning the art of graphics programming. I
recommend scouring the internet, hanging out in chat rooms such as
#SDL, and learning the SDL API in and out.

Please don’t hang out in #SDL, he only kidding I swear.On Jul 25, 2004, at 12:50 PM, Paul Lowe wrote:

Also, an excellent technique is to take apart other people’s programs
and look at how they did things!

Good luck,

Paul

Hi everyone. I’m new to SDL.

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.

~~~~~~~*
a header file kiosk.h

include <SDL/SDL_image.h>
#include <SDL/SDL.h>

using namespace std;

class Kiosk
{
public:

  SDL_Surface * screen;
  SDL_Rect pictureLocation;
  SDL_Surface * image;

 Kiosk()
 {
     screen = SDL_SetVideoMode(1024, 768, 16, SDL_DOUBLEBUF);
 }


  void showPicture(char * pic)
  {

     if(image != NULL){
        image = NULL;
     }

     image = IMG_Load(pic);

     pictureLocation.x = 100;
     pictureLocation.y = 0;

     SDL_BlitSurface(image, NULL, screen, &pictureLocation);
     SDL_Flip(screen);

  }

};

~~~~*~
some C++ code

#include “kiosk.h”

using namespace std;

SDL_Event event;
int change = 0;

int main(int argcs, char * pArgs[])
{
Kiosk k;
k.showPicture("…/Pictures/k_photos/green.jpg");

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;
}
}
else{
// right click
return 0;
}
default:
// nothing
break;
}
}
}

return 0;
}


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl