Sign in to follow this  
munchor

SDL and C++ Classes

Recommended Posts

I am having an issue with SDL and C++ classes. Basically, I want my Player to have a class of its own, so I made this (Player.cpp):

[code]
#include <SDL/SDL.h>

class Player {
int x;
int y;
};[/code]

I've got my main file, with the main () function, and the drawing functions. I wanted to call something like "Player player = new Player ();" (Java, Python and other languages allow me to do something like this).

I guessed that I would have to compile Player.cpp first, but it complains about not having a main function and whatnot.

So, I have a few questions:
- How can I have my own SDL Player class, and associate it with one or more sprites;
- I heard that I need header files to link the files, but I have no idea how;
- How to compile the different files, I am using a Makefile to build the game btw;

Thank you tons in advance.

Oh, and regarding question #1, I would also like to know how I can call the Player class from the main file so that then I can draw it.

Share this post


Link to post
Share on other sites
First of all, I would advise you read [url="http://www.gamedev.net/page/resources/_/technical/general-programming/organizing-code-files-in-c-and-c-r1798"]this article[/url]. Essentially, the class definition should be in a header file (e.g. Player.h). If the class requires source code, then you need an accompanying source file (e.g. Player.cpp). To use the class in a third file (e.g. Main.cpp), you need to #include the header file in this third file.

In C++, one would use "Player player;". Dynamic allocation (i.e. new/delete) is not necessary here.

Can you post your make file? It shouldn't be trying to compile and link Player.cpp on its own, it should be trying to compile Main.cpp and Player.cpp, and then link them together to produce your executable.

Share this post


Link to post
Share on other sites
Hi munchor,
Your doubts are basically C++ doubts instead of SDL related.
I'd totally consider studying C++ and Object Oriented Programming fist before really geting into SDL, OpenGL and etc.
Don't get me wrong, but I'm saying that because I was just like you, and could only really get things on my own when I decided to learn this topics.
If you already program, it's going to be really fast to learn, and I recommend the book "Sams teach yourself c plus plus in one hour a day". Really helped me to study by myself.

[]'s

Share this post


Link to post
Share on other sites
Hidden
[quote name='rip-off' timestamp='1329758414' post='4914864']
First of all, I would advise you read [url="http://www.gamedev.net/page/resources/_/technical/general-programming/organizing-code-files-in-c-and-c-r1798"]this article[/url]. Essentially, the class definition should be in a header file (e.g. Player.h). If the class requires source code, then you need an accompanying source file (e.g. Player.cpp). To use the class in a third file (e.g. Main.cpp), you need to #include the header file in this third file.

In C++, one would use "Player player;". Dynamic allocation (i.e. new/delete) is not necessary here.

Can you post your make file? It shouldn't be trying to compile and link Player.cpp on its own, it should be trying to compile Main.cpp and Player.cpp, and then link them together to produce your executable.
[/quote]

[code]
all:
@echo "** Building..."
g++ Player.cpp -o Player `sdl-config --cflags --libs` -lSDL_mixer -lSDL_ttf -lSDL_image
g++ mygame.cpp -o mygame `sdl-config --cflags --libs` -lSDL_mixer -lSDL_ttf -lSDL_image
clean:
@echo "** Removing object files and executable..."
rm -f mygame
install:
@echo '** Installing...'
cp mygame /usr/bin
uninstall:
@echo '** Uninstalling...'
rm mygame[/code]

That is my makefile.

I will read that article you sent me and will definitely change it, and based on the way the article looks like, it'll teach me everything I need to know, thank you so much, I've been looking for something like this for a few months now.


[quote name='returnONE' timestamp='1329758486' post='4914866']
Hi munchor,
Your doubts are basically C++ doubts instead of SDL related.
I'd totally consider studying C++ and Object Oriented Programming fist before really geting into SDL, OpenGL and etc.
Don't get me wrong, but I'm saying that because I was just like you, and could only really get things on my own when I decided to learn this topics.
If you already program, it's going to be really fast to learn, and I recommend the book "Sams teach yourself c plus plus in one hour a day". Really helped me to study by myself.

[]'s
[/quote]

Hi returnONE,

I understand exactly what you're saying. I don't need to study OOP, because, well, I know OOP already, I just happened to learn with implementations on other languages (Python, JS, Java, etc.). What I need to study, is how C++ handles classes and multiple files, and rip off gave me just the thing [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Thanks you two!

Share this post


Link to post
Thank you for the article rip off!

[b]Player.h[/b]
[code]
#ifndef PLAYER_H
#define PLAYER_H

class Player {
private:
Player() {};

public:
int x;
int y;
};

#endif
[/code]

[b]Player.cpp[/b]
[code]

#include "Player.h"

Player::Player (int player_x, int player_y) {
x = player_x;
y = player_y;
}[/code]

[b]Game.cpp[/b]
[code]#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>

#include "Player.h"

#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define SCREEN_DEPTH 8

using namespace std;

int main(int argc, char *argv[]);
void draw_player(SDL_Surface *screen, int x, int y);
void clear_screen(SDL_Surface *screen);

void draw_player(SDL_Surface *screen, int x, int y) {
/* Blits the player to the screen */

SDL_Rect player_block = {x, y, 25, 25};
Uint8 player_color = SDL_MapRGB(screen->format, 255, 255, 255);
SDL_FillRect(screen, &player_block, player_color);
}

void clear_screen(SDL_Surface *screen) {
/* Clear the screen to black */

Uint8 color_black = SDL_MapRGB(screen->format, 0, 0, 0);
SDL_FillRect(screen, NULL, color_black);
}

int main(int argc, char *argv[]) {
/* Initialize SDL */
if (SDL_Init(SDL_INIT_VIDEO) == -1)
printf("Error: unable to initialize SDL: '%s'\n", SDL_GetError());

SDL_Surface *screen;
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, SDL_SWSURFACE);
SDL_WM_SetCaption("Game", "Game");
SDL_Event event;
int running = 1;

Player player (20, 20);

/****************************** Main Game Loop ******************************/
while (running) {
SDL_PollEvent(&event);
if (event.type == SDL_QUIT) {
SDL_Quit();
return 0;
}
if (event.type == SDL_KEYDOWN) {
if (event.key.keysym.sym == SDLK_ESCAPE) {
SDL_Quit();
return 0;
}
}

draw_player(screen, player.x, player.y);

SDL_Flip(screen);
clear_screen(screen);
}

return 0;
}[/code]

I made those 3 files, I think they're correct, but I don't know how to compile/link them with g++. Any idea? Thanks.

Share this post


Link to post
Share on other sites
[quote name='Washu' timestamp='1329769939' post='4914935']
This depends on the compiler you've chosen to use, additionally if you're using an IDE or not.
[/quote]

I'm using g++, and no IDE.

I can compile Player to a .o file like this:

[code]g++ -c Player.cpp `sdl-config --cflags --libs` -lSDL_mixer -lSDL_ttf -lSDL_image[/code]

And I can compile my game with:

[code]g++ Game.cpp -o Game `sdl-config --cflags --libs` -lSDL_mixer -lSDL_ttf -lSDL_image[/code]

But my problem is the errors I get:


Player.cpp:3:1: error: redefinition of ‘Player::Player(int, int)’
Player.h:8:5: error: ‘Player::Player(int, int)’ previously defined here

I am using #ifnedf and #define to avoid this kind of errors, though, what's wrong?

Share this post


Link to post
Share on other sites
If you read the error (instead of just pasting it) you will see that its telling you that you defined the constructor twice. Which you did:
[CODE]
Player() {};
[/CODE]
[CODE]
Player::Player (int player_x, int player_y) {
x = player_x;
y = player_y;
}
[/CODE]

Furthermore, the signature of those constructors don't match. Lose the {} in the header, and add the parameters you're passing as well.

Share this post


Link to post
Share on other sites
Ah, so the {} was defining it already, I get it now.

Dropping the {} fixed it (and adding the arguments as well, but I had already done it ;)).

My problem now is that on my Game.cpp, I have "#include "Player.h"", and I call:

[code]Player player(20, 20);[/code]

However, the compiler returns:

[code]In function `main':
Game.cpp:(.text+0x133): undefined reference to `Player::Player(int, int)'
collect2: ld returned 1 exit status[/code]

I have Player.o on the directory, am I forgetting some linking, I am using this to compile:

[code]g++ Game.cpp -o Game `sdl-config --cflags --libs` -lSDL_mixer -lSDL_ttf -lSDL_image[/code]

I have all that because of SDL, yes.

Share this post


Link to post
Share on other sites
...
well, first off, seperate compilation from linking.

so compile game.cpp the same way you do player.cpp
then link them:
[CODE]g++ game.o player.o -o game[/CODE]

This is also why you should use a basic makefile, because you can simply add a couple of lines (or none if you use wildcards) and it'll take care of it.

Share this post


Link to post
Share on other sites
By invoking the toolchain like that, you are telling G++ that you have a full program in Game.cpp. You don't - some of the code is in Player.cpp. You can compile it using *.cpp, or by invoking the compiler like this:
[code]
$ g++ -c Game.cpp `sdl-config --cflags`
$ g++ -c Player.cpp `sdl-config --cflags`
$ g++ -o Ixlore Game.o Player.o `sdl-config --libs` -lSDL_mixer -lSDL_ttf -lSDL_image
[/code]
The first two commands invoke only the compiler part of the toolchain on each source file. The last part invokes the linker on the compiled object files.

This is more of a bash script than a makefile though. As Washu hints, you should perhaps do a bit more research into makefiles.

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