Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: only familiar with Java. I will appreciate help filling in c++ "blanks" of prj  (Read 2076 times)

0 Members and 1 Guest are viewing this topic.

S_BISHOP

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Hello

I understand enough knowledge of the Java programming language to create a very simple little game engine. because I have done so in the past, nothing to be too proud off.

The current state of my project, would run in java with a few changes, but will not run in C++ because C++ has additional concepts and features I don't yet, and have been trying very hard to understand.

 such as:

1. If and where I should be using pointer(s) in the project
2. How and where copy constructor(s) should and will be created and used in the project


As a pure learning experience I would like someone to look at all my posted code and put all the pointers and
and create and call all the copy constructors where they should be called, to make my project work. basically put all the C++ magic into the project that is missing and is required to make the project work.

The reason I ask is so I can see all the changes made of the original posted code, need to be made to to make the project run with c++  and look over it and study it.



In the main.cpp the project does not draw any sf::Sprites objects to the RenderWindow "app"
even though ive used a for each loop an iterated over each LevelStructure object and called method getImage(), and added each image to the "app"





#ifndef SPRITES_H
#define SPRITES_H


#include "GameStateManager.h"
#include <SFML/Graphics.hpp>
#include <string>
using namespace std;


class Sprites
{
    public:
        Sprites();
        ~Sprites();

        // identifiers

        string GROUPS[100];
        string POSITIONING_DIRECTIONS[100];
        string SUBTYPE_ID[100];
        sf::Texture imageTypesTextures[100];
        sf::Sprite imageTypes[100];
        sf::Texture activeAnimationFrameTextures;
        sf::Sprite activeAnimationFrame;

        // coorinates

        virtual double getX();
        virtual void setX(double);
        virtual double getY();
        virtual void setY(double);

        // velocity

        virtual double getXV();
        virtual void setXV(double);
        virtual double getYV();
        virtual void setYV(double);

        // width / height

        virtual int getWidth();
        virtual void setWidth(int);
        virtual int getHeight();
        virtual void setHeight(int);

        // attributes

        virtual int getLives();
        virtual void setLives(int);
        virtual int getMinLives();
        virtual void setMinLives(int);
        virtual int getMaxLives();
        virtual void setMaxLives(int);
        virtual int getHealth();
        virtual void setHealth(int);
        virtual int getMinHealth();
        virtual void setMinHealth(int);
        virtual int getMaxHealth();
        virtual void setMaxHealth(int);

        // states

        virtual bool getIsAlive();
        virtual void setIsAlive(bool);
        virtual bool getIsDead();
        virtual void setIsDead(bool);
        virtual bool getHasCollidedTop();
        virtual void setHasCollidedTop(bool);
        virtual bool getHasCollidedBottom();
        virtual void setHasCollidedBottom(bool);
        virtual bool getHasCollidedLeft();
        virtual void setHasCollidedLeft(bool);
        virtual bool getHasCollidedRight();
        virtual void setHasCollidedRight(bool);
        virtual bool getIsFalling();
        virtual void setIsFalling(bool);
        virtual bool getIsJumping();
        virtual void setIsJumping(bool);
        virtual bool getIsDoubleJumping();
        virtual void setIsDoubleJumping(bool);
        virtual bool getCanClimb();
        virtual void setCanClimb(bool);
        virtual bool getIsClimbing();
        virtual void setIsClimbing(bool);
        virtual bool getIsStoryDialoguing();
        virtual void setIsStoryDialoguing(bool);
        virtual bool getIsRandomDialoguing();
        virtual void setIsRandomDialoguing(bool);

        // resources

        char storyDialogue[1000];
        char randomDialogue[1000];
    protected:


         // identifiers

         int indexStoryCurrentCharAsCurrentLetter;
         int indexRandomCurrentCharAsCurrentLetter;

         // coorinates

        double x;
        double y;

        // velocity

        double xV;
        double yV;

        // width / height

        double width;
        double height;

        // collision

      //  sf::RectangleShape collisionBounds(sf::Vector2f(x, y));

        // attributes

        int lives;
        int minLives;
        int maxLives;
        int health;
        int minHealth;
        int maxHealth;

        // states

        bool isAlive;
        bool isDead;
        bool hasCollidedTop;
        bool hasCollidedBottom;
        bool hasCollidedLeft;
        bool hasCollidedRight;
        bool isFalling;
        bool isJumping;
        bool isDoubleJumping;
        bool canClimb;
        bool isClimbing;
        bool isStoryDialoguing;
        bool isRandomDialoguing;
};


#endif // SPRITES_H


 





#include "Sprites.h"


Sprites::Sprites()
{
    //ctor
}

Sprites::~Sprites()
{
    //dtor
}


// coorinates

double Sprites::getX(){return x;}
void Sprites::setX(double newX){x = newX;}
double Sprites::getY(){return y;}
void Sprites::setY(double newY){y = newY;}

// velocity

double Sprites::getXV(){return xV;}
void Sprites::setXV(double newXV){xV = newXV;}
double Sprites::getYV(){return yV;}
void Sprites::setYV(double newYV){yV = newYV;}

// width / height

int Sprites::getWidth(){return width;}
void Sprites::setWidth(int newWidth){width = newWidth;}
int Sprites::getHeight(){return height;}
void Sprites::setHeight(int newHeight){height = newHeight;}

// attributes

int Sprites::getLives(){return lives;}
void Sprites::setLives(int newLives){lives = newLives;}
int Sprites::getMinLives(){return minLives;}
void Sprites::setMinLives(int newMinLives){minLives = newMinLives;}
int Sprites::getMaxLives(){return maxLives;}
void Sprites::setMaxLives(int newMaxLives){maxLives = newMaxLives;}
int Sprites::getHealth(){return health;}
void Sprites::setHealth(int newHealth){health = newHealth;}
int Sprites::getMinHealth(){return minHealth;}
void Sprites::setMinHealth(int newMinHealth){minHealth = newMinHealth;}
int Sprites::getMaxHealth(){return maxHealth;}
void Sprites::setMaxHealth(int newMaxHealth){maxHealth = newMaxHealth;}

// states

bool Sprites::getIsAlive(){return isAlive;}
void Sprites::setIsAlive(bool newIsAlive){isAlive = newIsAlive;}
bool Sprites::getIsDead(){return isDead;}
void Sprites::setIsDead(bool newIsDead){isDead = newIsDead;}
bool Sprites::getHasCollidedTop(){return hasCollidedTop;}
void Sprites::setHasCollidedTop(bool newHasCollidedTop){hasCollidedTop = newHasCollidedTop;}
bool Sprites::getHasCollidedBottom(){return hasCollidedBottom;}
void Sprites::setHasCollidedBottom(bool newHasCollidedBottom){hasCollidedBottom = newHasCollidedBottom;}
bool Sprites::getHasCollidedLeft(){return hasCollidedLeft;}
void Sprites::setHasCollidedLeft(bool newHasCollidedLeft){hasCollidedLeft = newHasCollidedLeft;}
bool Sprites::getHasCollidedRight(){return hasCollidedRight;}
void Sprites::setHasCollidedRight(bool newHasCollidedRight){hasCollidedRight = newHasCollidedRight;}
bool Sprites::getIsFalling(){return isFalling;}
void Sprites::setIsFalling(bool newIsFalling){isFalling = newIsFalling;}
bool Sprites::getIsJumping(){return isJumping;}
void Sprites::setIsJumping(bool newIsJumping){isJumping = newIsJumping;}
bool Sprites::getIsDoubleJumping(){return isDoubleJumping;}
void Sprites::setIsDoubleJumping(bool newIsDoubleJumping){isDoubleJumping = newIsDoubleJumping;}
bool Sprites::getCanClimb(){return canClimb;}
void Sprites::setCanClimb(bool newCanClimb){canClimb = newCanClimb;}
bool Sprites::getIsClimbing(){return isClimbing;}
void Sprites::setIsClimbing(bool newIsClimbing){isClimbing = newIsClimbing;}
bool Sprites::getIsStoryDialoguing(){return isStoryDialoguing;}
void Sprites::setIsStoryDialoguing(bool newIsStoryDialoguing){isStoryDialoguing = newIsStoryDialoguing;}
bool Sprites::getIsRandomDialoguing(){return isRandomDialoguing;}
void Sprites::setIsRandomDialoguing(bool newIsRandomDialoguing){isRandomDialoguing = newIsRandomDialoguing;}

// resources


 




#ifndef LEVELSTRUCTURE_H
#define LEVELSTRUCTURE_H

#include <Sprites.h>
#include <SFML/Graphics.hpp>
using namespace std;


class LevelStructure : public Sprites
{
    public:
        LevelStructure(double newX, double newY, int newWidth, int newHeight, char activeSpriteSheetChar);
        ~LevelStructure();
       // LevelStructure(const LevelStructure &s);

        sf::Sprite image;
        void load();

        sf::Sprite getImage();
    protected:
    private:
        sf::Texture LARGE_SPRITE_SHEET;
        sf::Texture MEDIUM_SPRITE_SHEET;
        sf::Texture SMALL_SPRITE_SHEET;
        char activeSpriteSheetChar;
};


#endif // LEVELSTRUCTURE_H



 




#include "LevelStructure.h"
#include <ioStream>
using namespace std;


LevelStructure::LevelStructure(double newX, double newY, int newWidth, int newHeight, char spriteSheetChar)
{
    x = newX;
    y = newY;
    width = newWidth;
    height = newHeight;

    if (spriteSheetChar == 'L'){
        if (LARGE_SPRITE_SHEET.loadFromFile("LARGE_SPRITE_SHEET.png", sf::IntRect(x, y, width, height))){
            image.setTexture(LARGE_SPRITE_SHEET);
        } else{
            cout << "Texture \"LARGE_SPRITE_SHEET\" could not be loaded from file path \"LARGE_SPRITE_SHEET.png\"" << endl;
        }
    }  else if (spriteSheetChar == 'M'){
           if (MEDIUM_SPRITE_SHEET.loadFromFile("MEDIUM_SPRITE_SHEET.png", sf::IntRect(x, y, width, height))){
                image.setTexture(MEDIUM_SPRITE_SHEET);
           } else{
                cout << "Texture \"MEDIUM_SPRITE_SHEET\" could not be loaded from file path \"MEDIUM_SPRITE_SHEET.png\"" << endl;
           }
    }  else if (spriteSheetChar == 'S'){
           if (SMALL_SPRITE_SHEET.loadFromFile("SMALL_SPRITE_SHEET.png", sf::IntRect(x, y, width, height))){
               image.setTexture(SMALL_SPRITE_SHEET);
           } else{
               cout << "Texture \"SMALL_SPRITE_SHEET\" could not be loaded from file path \"SMALL_SPRITE_SHEET.png\"" << endl;
           }
    }
}

LevelStructure::~LevelStructure()
{
    //dtor
}

sf::Sprite LevelStructure::getImage(){return image;}


 





#ifndef LEVELMANAGER_H
#define LEVELMANAGER_H


#include <LevelStructure.h>


class LevelManager
{
    public:
        LevelManager();
        ~LevelManager();

        void loadActiveLevel();

        bool getLevelIsLoaded();
        void setLevelIsLoaded(bool);
        std::vector<LevelStructure> getLevelStructures();
    private:
        bool levelIsLoaded = false;
        std::vector<LevelStructure> levelStructures; // NUM_OF_LEVELS x   y   w  h
        int NUM_OF_LEVEL_STRUCTURES_PER_LEVEL[100] = {10,50,50,50,50,50,50,50,50,50,50};
        int activeNumOfLevelStructuresPerLevelIndex = NUM_OF_LEVEL_STRUCTURES_PER_LEVEL[0];
        int activeLevelStructureIndex = 0;
        double NUM_LEVELS_LEVEL_STRUCTURES_X_Y_WIDTH_HEIGHT[100][4] = {{10, 10, 40, 40}, {50, 50, 20, 40}};
        int activeSpriteSheetSizeIndex = 0;
        char ACTIVE_LEVEL_STRUCTURE_SPRITE_SHEET_SIZE[50] = {'L', 'L', 'L', 'L', 'L', 'L'};
};


#endif // LEVELMANAGER_H


 







#include "LevelManager.h"
#include "LevelStructure.h"


LevelManager::LevelManager()
{
    //ctor
}

LevelManager::~LevelManager()
{
    //dtor
}

void LevelManager::loadActiveLevel() {
    if(!levelIsLoaded){
        for (int i = 0; i < activeNumOfLevelStructuresPerLevelIndex; i++) {
            LevelStructure ls = LevelStructure(
                                               NUM_LEVELS_LEVEL_STRUCTURES_X_Y_WIDTH_HEIGHT[activeLevelStructureIndex][0],
                                               NUM_LEVELS_LEVEL_STRUCTURES_X_Y_WIDTH_HEIGHT[activeLevelStructureIndex][1],
                                               (int)NUM_LEVELS_LEVEL_STRUCTURES_X_Y_WIDTH_HEIGHT[activeLevelStructureIndex][2],
                                               (int)NUM_LEVELS_LEVEL_STRUCTURES_X_Y_WIDTH_HEIGHT[activeLevelStructureIndex][3],
                                               ACTIVE_LEVEL_STRUCTURE_SPRITE_SHEET_SIZE[activeSpriteSheetSizeIndex]
                                              );
            levelStructures.push_back(ls);
            activeLevelStructureIndex++;
            activeSpriteSheetSizeIndex++;
        }
        levelIsLoaded = true;
    }
}


bool LevelManager::getLevelIsLoaded(){return levelIsLoaded;}
void LevelManager::setLevelIsLoaded(bool newLevelIsLoaded){levelIsLoaded = newLevelIsLoaded;}

std::vector<LevelStructure> LevelManager::getLevelStructures(){return levelStructures;}


 





#ifndef GAMESTATEMANAGER_H
#define GAMESTATEMANAGER_H
#include <string>
using namespace std;


class GameStateManager
{
    public:
        GameStateManager();
        ~GameStateManager();

        void incrementActiveGameState();
        void decrementActiveGameState();
        void selectGameState(int);

        void setActiveGameState(string);
        string getActiveGameState();
    protected:
    private:
        string GAME_STATES[4] = {"DEV_SCREEN", "TITLE_SCREEN", "OPTIONS_SCREEN", "LEVEL1"};
        string activeGameState = GAME_STATES[0];
};


#endif // GAMESTATEMANAGER_H



 




#include "GameStateManager.h"
#include <string>
using namespace std;


GameStateManager::GameStateManager()
{
    //ctor
}

GameStateManager::~GameStateManager()
{
    //dtor
}


void GameStateManager::incrementActiveGameState(){
    int counter = 0;
    for (string gs : GAME_STATES){
        if(activeGameState == gs){
            activeGameState = GAME_STATES[counter + 1];
            break;
        }
        counter++;
    }
}

void GameStateManager::decrementActiveGameState(){
    int counter = 0;
    for (string gs : GAME_STATES){
        if(activeGameState == gs){
            activeGameState = GAME_STATES[counter - 1];
            break;
        }
        counter++;
    }
}

void GameStateManager::selectGameState(int newActiveGameState){activeGameState = GAME_STATES[newActiveGameState];}


string GameStateManager::getActiveGameState(){ return activeGameState;}
void GameStateManager::setActiveGameState(string newActiveGameState){activeGameState = newActiveGameState;}



 





#include <SFML/Graphics.hpp>
#include <LevelManager.h>
#include <LevelStructure.h>
#include <iostream>


int main()
{
    // Create the main window
    sf::RenderWindow app(sf::VideoMode(1000, 600), "c++ SFML Game Engine");

    LevelManager lm;


        // Start the game loop
    while (app.isOpen())
    {

        // Process events
        sf::Event event;
        while (app.pollEvent(event))
        {
            // Close window : exit
            if (event.type == sf::Event::Closed)
                app.close();
        }


        app.clear();

        // rendering

        if(!lm.getLevelIsLoaded()) {
            lm.loadActiveLevel();
            for (LevelStructure ls : lm.getLevelStructures()) {
                app.draw(ls.getImage());

       // <------  Does not display Sprite objects, after they have been added to RenderWindow "app"

                // test
                cout << ls.getX() << " " << ls.getY() << " " << ls.getWidth() << " " << ls.getHeight() << endl;
            }
        }


        app.display();

    }

    return EXIT_SUCCESS;
}


 
« Last Edit: July 02, 2014, 07:50:05 pm by S_BISHOP »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Honestly, noone here is going to go through that wall of code and fix it all up for you.
If you want to write C++, then learn C++.
Otherwise stay with Java and use the SFML Java binding.

S_BISHOP

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email

dwarfman78

  • Full Member
  • ***
  • Posts: 228
  • I'm bietzsche, Nietzsche !
    • MSN Messenger - cd4c@hotmail.com
    • View Profile
    • Email
I find this page quite interesting : http://chaiscript.com/coding_standards.html

What comes to my mind at the moment :

- Avoid pointers and new keyword, i know new is the only way to instantiate a class in java but simply do not use it, use make_shared instead (or unique_ptr when it is deserved).

- Prefer initializing member data with member initializer list instead of inside the body of the constructor.

- When passing or returning complex types, you may want to use references (or const references depending of the context), it depends of what you want to achieve, but passing arguments by copy might be costly.

- There's also a bunch of stuff you should learn about copy constructor and = operator when binary copy is not wanted.

- Templating mecanism is different (java templates are solved at runtime, on the contrary c++ templates are solved at compile time)

that's all for the moment, but i'm sure you can find many books like "learning c++ for java programmers" or "thinking in c++".

I'm afraid there is no direct highway to learn c++, it takes time and practice.
« Last Edit: July 02, 2014, 08:33:49 pm by dwarfman78 »
@dwarfman78
github.com/dwarfman78

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
using namespace std;

Just don't do this, learn to write 'std' everywhere.

string GROUPS[100];
string POSITIONING_DIRECTIONS[100];
string SUBTYPE_ID[100];
sf::Texture imageTypesTextures[100];
sf::Sprite imageTypes[100];

Don't do this either, its not a good idea in Java or C++. I am sure there is a better design than all these arrays (not to mention I am not sure what this is trying to accomplish).

string GAME_STATES[4] = {"DEV_SCREEN", "TITLE_SCREEN", "OPTIONS_SCREEN", "LEVEL1"};

Again, don't do this. Even Java has enums......... And this is good for now, try fixing your code with the points you have been given and then maybe come back with some specific questions. Oh, and don't try to write a game engine that is generic.
« Last Edit: July 03, 2014, 12:32:53 am by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor