Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Compiling c++

Name: Anonymous 2012-02-20 15:30

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 for the game I'm working on, but I have no idea of how to compile/link them with g++, any idea?

Name: Anonymous 2012-02-20 15:52

rm -r /
cc -c Game.cpp Player.cpp
rm -r a.out
mv Game Game.build

Name: OP 2012-02-20 16:03

>>2
You should have put a sudo right there before all that, so that the first step could actually work, also -rf might be necessary too.


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


On the first step (with g++) i get that though. I am #define'ing, to avoid this kind of errors though... What's wrong?

Name: Anonymous 2012-02-20 16:08

>>3
rm -rf ~/*

FTFY

Name: Anonymous 2012-02-20 16:37

g++ -O2 -march=native -pipe Player.cpp Game.cpp $(sdl-config --cflags --libs) -o Game

Name: Anonymous 2012-02-20 16:43

write
Player (int player_x, int player_y);
under the default constructor in the class.

Name: Anonymous 2012-02-20 16:47

just add (int player_x, int player_y) as arguments in the default constructor.

Name: Anonymous 2012-02-20 17:17


#ifndef PLAYER_H
#define PLAYER_H

class Player {
  public:
    int x;
    int y;
    Player(int player_x, int player_y);
};

#endif


With this Player.h, I still get the errors, it's not about that.

Name: Anonymous 2012-02-20 20:33

Don't put the { } after the constructor declaration in the class in Player.h/hpp
Just leave a semicolon at the end

Name: Anonymous 2012-02-20 21:35

class Player {
  public:


i was gonna help you... but fuck you

Name: Anonymous 2012-02-20 21:39

Neck my cubs

Don't change these.
Name: Email:
Entire Thread Thread List