Storing breakout bricks in an array SDL

Started by
7 comments, last by SiCrane 10 years, 9 months ago

So I just started game programming and I was working on a game like breakout where there is a paddle and a set of bricks that can be destroyed using the ball.I am using SDL and C++ to make the game. I can't figure how out how to implement the set of bricks in the game.I have a class "Brick" .I use a constructor to initialize the values of the SDL_Rect for the brick and also the surface image for the brick. Also I use a show function to show the brick using the SDL_BlitSurface function. Now I declare an array say "brick a[20];" and even if I don't call no function or constructor , on compiling it fails to compile and gives an error. I am using Visual studio 2010.

Advertisement

Show the code giving you problems (specifically the Brick class, and how you're trying to initialize the array), and show the errors you are getting.

Is your class Brick or brick? Also, does the Brick constructor have non-default parameters? If so, simply calling "Brick a[20];" won't work since the compiler expects parameters passed to Brick.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Main.cpp:

#include <SDL.h>
#include <iostream>
#include "player.h"
#include "ball.h"
#include "brick.h"
#include <vector>

using namespace std;
SDL_Surface* load_image(const char* c,Uint32 colorkey=0)
{
SDL_Surface* tmp=SDL_LoadBMP(c);
if(colorkey!=0)
{
SDL_SetColorKey(tmp,SDL_SRCCOLORKEY,colorkey);
}
return tmp;
}



int main( int argc, char* args[] )
{
//Start SDL

SDL_Surface* screen,*background;
const int width=640;
const int FPS=30;
const int height=480;
screen=SDL_SetVideoMode(width,height,32,SDL_SWSURFACE);
SDL_Event event;
Uint32 start;
player player1(load_image("pappu.bmp"),0,SDL_GetVideoSurface()->clip_rect.h-10,50,10,10);
ball ball1(load_image("ball.bmp"),width/2,height/2,15,15,2,2);
brick brick1(load_image("brick.bmp"),40,200,40,20);
brick bricks[20]; //array declaration

/*for(int i=40,j=0;i<=600 && j<=20;i+=40,j++){
brickv[j].set(load_image("brick.bmp"),i,380,40,20);

}
/*background=SDL_LoadBMP("background.bmp");
SDL_Rect camera;
camera.x=0;
camera.y=0;
camera.w=640;
camera.h=480;
*/
bool running=true;
int arr[4]={0,0,0,0};
while(running){

start=SDL_GetTicks();
while(SDL_PollEvent(&event)){
switch(event.type){
case SDL_QUIT:
running=false;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym){
case SDLK_RIGHT:
arr[0]=1;
break;
case SDLK_LEFT:
arr[1]=1;
break;
}
break;
case SDL_KEYUP:
switch(event.key.keysym.sym){
case SDLK_RIGHT:
arr[0]=0;
break;
case SDLK_LEFT:
arr[1]=0;
break;
}
break;
}
}
if(arr[0]==1){
player1.moveR();



}
if(arr[1]==1){
player1.moveL();
}

SDL_FillRect(screen,&screen->clip_rect,SDL_MapRGB(screen->format,0xff,0xff,0xff));


player1.show();
ball1.show();
ball1.move(player1.getRect());
ball1.walls();
brick1.show();


SDL_Flip(screen);
//regulate FPS
if(1000/FPS>(SDL_GetTicks()-start))
SDL_Delay(1000/FPS-(SDL_GetTicks()-start));

}




//Quit SDL
SDL_Quit();

return 0;
}






brick.h:


#include <SDL.h>

#ifndef brick_h
#define brick_h

class brick{
SDL_Rect brik;
SDL_Surface* image;
public:
brick();
brick(SDL_Surface* img,int x,int y,int w,int h);
~brick();
void show();
SDL_Rect* get_Rect();
void Free();
void set(SDL_Surface* img,int x,int y,int w,int h);
};


#endif




brick.cpp:


#include "brick.h"

brick::brick(SDL_Surface* img,int x,int y,int w,int h){
image=img;
brik.x=x;
brik.y=y;
brik.w=w;
brik.h=h;


}

brick::~brick(){
SDL_FreeSurface(image);
}

void brick:: show(){
SDL_BlitSurface(image,NULL,SDL_GetVideoSurface(),&brik);

}
SDL_Rect* brick::get_Rect(){
return &brik;

}

void brick::Free(){
SDL_LockSurface(image);

}
Error :


1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall brick::brick(void)" (??0brick@@QAE@XZ) referenced in function _SDL_main
1>F:\Opengl\Projects\myfirstgame\Debug\myfirstgame.exe : fatal error LNK1120: 1 unresolved externals

The error is telling you the problem. You have a default constructor declared in the brick class definition, but you don't provide a definition for the constructor. In other words, you need a function body for your brick() function.

I had to add the default constructor because the compiler wouldn't let me initialize an array otherwise.
Okay Okay! I get it. Thanks! It worked! smile.png
And can I not initialize the data values of the members of the array using the parameterized constructor?
You can:

brick bricks[20] = { brick(/* whatever */), brick(/* whatever */), brick(/* whatever */), brick(/* whatever */), ... };
On a side note, you should probably look into either implementing or disabling the copy constructor and assignment operator for your brick class. See the rule of three.

This topic is closed to new replies.

Advertisement