Jump to content

  • Log In with Google      Sign In   
  • Create Account

SDL and C++ Classes


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 munchor   Members   -  Reputation: 101

Like
0Likes
Like

Posted 20 February 2012 - 09:48 AM

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;

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.

Sponsor:

#2 rip-off   Moderators   -  Reputation: 8727

Like
0Likes
Like

Posted 20 February 2012 - 11:20 AM

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.

#3 returnONE   Members   -  Reputation: 100

Like
0Likes
Like

Posted 20 February 2012 - 11:21 AM

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

#4 munchor   Members   -  Reputation: 101

Like
0Likes
Like

Posted 20 February 2012 - 02:28 PM

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.

#5 Washu   Senior Moderators   -  Reputation: 5423

Like
0Likes
Like

Posted 20 February 2012 - 02:32 PM

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

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#6 munchor   Members   -  Reputation: 101

Like
0Likes
Like

Posted 20 February 2012 - 03:13 PM

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?

#7 Washu   Senior Moderators   -  Reputation: 5423

Like
0Likes
Like

Posted 20 February 2012 - 03:16 PM

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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#8 munchor   Members   -  Reputation: 101

Like
0Likes
Like

Posted 20 February 2012 - 03:25 PM

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.

#9 Washu   Senior Moderators   -  Reputation: 5423

Like
0Likes
Like

Posted 20 February 2012 - 03:37 PM

...
well, first off, seperate compilation from linking.

so compile game.cpp the same way you do player.cpp
then link them:
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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#10 rip-off   Moderators   -  Reputation: 8727

Like
0Likes
Like

Posted 20 February 2012 - 04:02 PM

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:
$ 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
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.

#11 munchor   Members   -  Reputation: 101

Like
0Likes
Like

Posted 20 February 2012 - 04:35 PM

Thank you, I already fixed everything =D




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS