• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Jaydeep Hora

Storing breakout bricks in an array SDL

8 posts in this topic

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites
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);

}
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites
I had to add the default constructor because the compiler wouldn't let me initialize an array otherwise.
0

Share this post


Link to post
Share on other sites
And can I not initialize the data values of the members of the array using the parameterized constructor?
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0