• 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
munchor

SDL and C++ Classes

10 posts in this topic

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.
0

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.
0

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
0

Share this post


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

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.
0

Share this post


Link to post
Share on other sites
This depends on the compiler you've chosen to use, additionally if you're using an IDE or not.
0

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?
0

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.
0

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.
0

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.
0

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