#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:
_________________
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
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