# SDL and C++ Classes

This topic is 2792 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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

 #include <SDL/SDL.h> class Player { int x; int y; };

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;

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 on other sites
First of all, I would advise you read this article. 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 on other sites
Hi munchor,
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 on other sites
Hidden

First of all, I would advise you read this article. 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.

 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

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.

Hi munchor,
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

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

Thanks you two!

Thank you for the article rip off!

Player.h
 #ifndef PLAYER_H #define PLAYER_H class Player { private: Player() {}; public: int x; int y; }; #endif 

Player.cpp
 #include "Player.h" Player::Player (int player_x, int player_y) { x = player_x; y = player_y; }

Game.cpp
#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; }

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 on other sites
This depends on the compiler you've chosen to use, additionally if you're using an IDE or not.

##### Share on other sites

This depends on the compiler you've chosen to use, additionally if you're using an IDE or not.

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

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

g++ -c Player.cpp sdl-config --cflags --libs -lSDL_mixer -lSDL_ttf -lSDL_image

And I can compile my game with:

g++ Game.cpp -o Game sdl-config --cflags --libs -lSDL_mixer -lSDL_ttf -lSDL_image

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 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:
 Player() {}; 
 Player::Player (int player_x, int player_y) { x = player_x; y = player_y; } 

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

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

Player player(20, 20);

However, the compiler returns:

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

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

g++ Game.cpp -o Game sdl-config --cflags --libs -lSDL_mixer -lSDL_ttf -lSDL_image

I have all that because of SDL, yes.

##### Share on other sites
...
well, first off, seperate compilation from linking.

so compile game.cpp the same way you do player.cpp
g++ game.o player.o -o game

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.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 13
• 30
• 9
• 16
• 12