gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > My first graphics in SDL/C++

#141864 - Dood77 - Sun Sep 30, 2007 10:39 pm

I've been attempting to learn C++ on my own time with online tutorials, soon I hope to get a nice book, etc.
I've programmed a few console apps that do simple math, etc.
And now I've tried to program some graphics with SDL, I did this tutorial that sets up the screen and fills it with a colorful background, and now I'm trying to code a simple pong game, but I'm having trouble with some object-oriented things.

So far I have my struct called box.
Box contains x, y, height, width ints, as well as two color structs called innerColor and borderColor, each with ints r, g, and b.
I want box to have a function called draw that will use two for loops to draw its pixels on to the buffer, but this is where I'm having the trouble.
In the main function, I have a while loop that loops until the int done == 1, and in that loop, I create an instance of the box struct called p1, I set p1.x, p1.y, p1.width, etc. I then want to be able to call p1::draw(buffer) but I'm getting some really strange compiler errors.
Oh yeah, and I'm compiling using Dev-C++ on win32
Heres my messy source:
Code:
#include <stdlib.h>
#include <iostream.h>

#include <SDL/SDL.h>
using namespace std;
// Check if screen needs to be locked and lock it on func call:
void Slock(SDL_Surface *screen)
{
  if ( SDL_MUSTLOCK(screen) )
  {
    if ( SDL_LockSurface(screen) < 0 )
    {
      return;
    }
  }
}
//unlock screen:
void Sulock(SDL_Surface *screen)
{
  if ( SDL_MUSTLOCK(screen) )
  {
    SDL_UnlockSurface(screen);
  }



//function to draw a pixel as dictated by SDL tutorial
void DrawPixel(SDL_Surface *screen, int x, int y,
                                    Uint8 R, Uint8 G, Uint8 B)
{
  Uint32 color = SDL_MapRGB(screen->format, R, G, B);
  switch (screen->format->BytesPerPixel)
  {
    case 1: // Assuming 8-bpp
      {
        Uint8 *bufp;
        bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
        *bufp = color;
      }
      break;
    case 2: // Probably 15-bpp or 16-bpp
      {
        Uint16 *bufp;
        bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
        *bufp = color;
      }
      break;
    case 3: // Slow 24-bpp mode, usually not used
      {
        Uint8 *bufp;
        bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;
        if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
        {
          bufp[0] = color;
          bufp[1] = color >> 8;
          bufp[2] = color >> 16;
        } else {
          bufp[2] = color;
          bufp[1] = color >> 8;
          bufp[0] = color >> 16;
        }
      }
      break;
    case 4: // Probably 32-bpp
      {
        Uint32 *bufp;
        bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
        *bufp = color;
      }
      break;
  }
}
//structs
struct color
{
       int r;
       int g;
       int b;
}
struct box
{
       int width;
       int height;
       int x;
       int y;
       struct color borderColor;
       struct color innerColor;
//heres where I attempt to have the draw function in box
       void box::draw(SDL_Surface *screen)
       {
         for (x;x<x+width;x++)
         {
              for (y;y<y+height;y++)
                 {
                        DrawPixel(screen,x,y,innerColor.r,innerColor.g,innerColor.b);
                 }
         }
       }     
}


   

//draw background
void DrawScene(SDL_Surface *screen)
{

  Slock(screen);
  for(x=0;x<32;x++)
  {
    for(y=0;y<32;y++)
    {
       DrawPixel(screen, x, y, 255, 255, 255);
    }
  }

  Sulock(screen);
  SDL_Flip(screen);
}

//----------------MAIN------------------
int main(int argc, char *argv[])
{
                   //error checking
  if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
  {
    cout << "Unable to init SDL: " << SDL_GetError() <<endl;
    exit(1);
  }
  atexit(SDL_Quit);
                   //setup surface
  SDL_Surface *screen;
  screen=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
                   //error check surface
  if ( screen == NULL )
  {
    cout << "Unable to set 640x480 video: " << SDL_GetError() << endl;
    exit(1);
  }

int done = 0;
    //----------Game loop-----------
while (done == 0)
{
      SDL_Event event;

    while ( SDL_PollEvent(&event) )
    {
      if ( event.type == SDL_QUIT )  {  done = 1;  }
/*
      if ( event.type == SDL_KEYDOWN )
      {
        cout << event.key.keysym.sym << endl << endl;
        done = 0;
      } */
    }
    struct box p1;
    p1.width = 32;
    p1.height = 8;
    p1.borderColor.r = 0;
    p1.borderColor.g = 0;
    p1.borderColor.b = 0;
    p1.innerColor.r = 0;
    p1.innerColor.g = 200;
    p1.innerColor.b = 0;
    p1::draw(screen);
    DrawScene(screen);
}
//program end
  return 0;
}


_________________
If I use a term wrong or something then feel free to correct, I?m not much of a programmer.

Original DS Phat obtained on day of release + flashme v7
Supercard: miniSD, Kingston 1GB, Kingston 2GB
Ralink chipset PCI NIC

#141925 - gauauu - Mon Oct 01, 2007 7:18 pm

I haven't used C++ structs with methods in them much, so maybe I'm smoking crack, but why are you calling draw() as a static method instead of as a member. Instead, try calling it as a member:


Code:

...
    p1.innerColor.g = 200;
    p1.innerColor.b = 0;
    p1.draw(screen);


edit:added text to clarify that might code snippet was a correction.


Last edited by gauauu on Mon Oct 01, 2007 10:20 pm; edited 1 time in total

#141928 - tepples - Mon Oct 01, 2007 7:41 pm

gauauu wrote:
I haven't used C++ structs with methods in them much

You may have used something identical and just not know it. As StoneCypher has explained several times, the struct and class keywords of the C++ language differ only in the privilege of any members that precede the first privilege label: public for structs and private for classes. Semantics in code designed to be compiled in both the C++ language and the C language, such as the header file of a library, are more complicated.

EDIT: Removed the second part of my post where I was confusing a quotation with a correction.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.


Last edited by tepples on Mon Oct 01, 2007 7:57 pm; edited 1 time in total

#141929 - Lick - Mon Oct 01, 2007 7:54 pm

tepples: near the end of the original source listing...
Quote:
p1::draw(screen);


By the way this is just funny naming of variables:
Quote:
event.key.keysym.sym

_________________
http://licklick.wordpress.com

#141945 - Dood77 - Tue Oct 02, 2007 3:20 am

Thanks for the help, I figured it all out today at school, and the school blocks almost all forums so I probably would've figured it out sooner were it not for my school's blind sensorship... But it turns out the problem mentioned was one of them, but my main problem was that I required semicolons after the block of code defining my structs. I read nothing of this in my earlier more basic tutorials, and the compiler errors were really unrelated to this problem.

The other problem I was having with this function was SDL throwing a "segmentation fault" and terminating the program (or in stderr.txt's words, "SDL Parachute deployed", lol). This was caused by the rather daft blunder of creating an infinite for loop like so:
Code:

for (x=0;x<x+width;x++)
{
//blah
}

_________________
If I use a term wrong or something then feel free to correct, I?m not much of a programmer.

Original DS Phat obtained on day of release + flashme v7
Supercard: miniSD, Kingston 1GB, Kingston 2GB
Ralink chipset PCI NIC