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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - packetpirate

Pages: [1]
1
Graphics / Re: Unknown problem with drawing.
« on: November 26, 2013, 12:38:56 am »
Third,  what global variables?

Line 7 in SFMLApp.cpp:
TextureManager* SFMLApp::textureManager = new TextureManager();

As for the "unnecessary" include files... what's unnecessary about them? I can't very well use an SFML class without including the appropriate header...

Just include what you need. If you need the RenderWindow, you include <SFML/Graphics/RenderWindow.hpp>. If you need a Sprite, you include <SFML/Graphics/Sprite.hpp> and so on.

It makes the include part look longer and it takes time getting used to, but it speeds up the building process.

and yes, the Init() method, rather than a constructor was necessary... how else do you expect me to return a boolean value to tell the Execute() method whether or not the window was properly initialized?

Exceptions

But back to the topic:
Try to print out the scale, position and size of the tile your mouse is hovering over or even put the whole tilemap into a text file. Then investigate which variable is wrong. I could even imagine that there are tiles with no texture over the actual tiles.

P.S.: Nexus is right, your map generation algorithm is short, but complex and error-prone. Look here or here for some ideas.

That's not a global variable... that's a static member of the SFMLApp class.
As for the includes, I'll do that. I didn't realize you could include sub-classes from those packages.

2
Graphics / Re: Unknown problem with drawing.
« on: November 25, 2013, 05:48:57 pm »
bool turn = (((rand() % 20) + 1) > 9);
xDir = ((((cX + xDir) >= 0) && ((cX + xDir) < this->level->GetWidth()))?xDir:-xDir);
 
When you write code like that, I'm not surprised you don't understand it...

If you just want a random amount of tiles to be filled, this can be done much simpler. What random pattern do you want to use exactly? Let's rewrite it from scratch, that's faster.

Also, there are a lot of other questionable things in your code that are certain to create problems in the long term. I really suggest to fix those things, as they will turn maintenance into hell as soon as the project grows. For example:
  • Global variables
  • Manual memory management. In modern C++, you don't need new and delete! RAII is the keyword here.
  • Init/Cleanup functions instead of constructors and destructors
  • Arrays -> use std::array
  • Unnecessary include files (#include <SFML/Graphics.hpp> increases compile time unnecessarily)
  • Undefined behavior (this->tiles[r][c].~Tile(); -- you may not call destructors explicitly), there's probably more UB
  • No const-correctness (getters such as GetWidth() should be const-qualified)
  • Useless virtual destructors although you don't have polymorphism
  • Disregarded rule of three/five

First of all, I understand everything in that snippet you posted. Second, it has been a while since I've used C++. I've been focusing on Java for a couple years, so I'm get re acclimated to pointers. Third,  what global variables?

As for the "unnecessary" include files... what's unnecessary about them? I can't very well use an SFML class without including the appropriate header... and yes, the Init() method, rather than a constructor was necessary... how else do you expect me to return a boolean value to tell the Execute() method whether or not the window was properly initialized?

And finally, as for the random level generation, I don't have anything specific in mind. The only algorithm I've looked at so far is BSP generation, but I don't really understand that yet. One thing at a time.

3
Graphics / Unknown problem with drawing.
« on: November 25, 2013, 04:41:12 pm »
I'm having some trouble with drawing tiles from a tile engine I'm working on. I'm honestly not sure if it's just a fluke, or if there's something causing it, but I wrote a quick and dirty level generator that isn't meant to actually generate anything special... it's just meant to show random tiles so I can play around with the tiles themselves. The problem, however, is that sometimes, I'll get tiles that half-appear, or appear to push other tiles over a random number of pixels.

This only seems to happen after I re-generate the level 5+ times, but hey... it's random, so who knows, right?

So is there something I'm doing wrong here?

SFMLApp.h: http://pastebin.com/67LNpf9V
SFMLApp.cpp: http://pastebin.com/NrjQKSKz
Level.h: http://pastebin.com/Ve4FET96
Level.cpp: http://pastebin.com/60WXYBfA
Tile.h: http://pastebin.com/DSU6C5xM
Tile.cpp: http://pastebin.com/C5eG5q6J

Seems like a lot of code to ask you to review, but really all you should need to look at is the GenerateLevel() method of the SFMLApp class and the Tile class' constructor and Draw() method. I just included the rest in case you need to see how I'm handling something specific.

I've attached some screenshots of the problem itself. All tiles should be the same distance from each other... 48 px, and if you'll notice, some of the tiles are pushed over slightly, or the tile's image itself will even be cut off.

Can anyone take a guess as to what's causing this?

4
Graphics / Re: Integer Division by Zero Exception when Drawing Shape?
« on: November 22, 2013, 08:40:00 pm »
Ok, so I had the idea that maybe loading the same texture so many times might be causing issues, so I created a texture manager class to load the texture once, and then pass a pointer to it to the Tile class constructor to be used in setting the texture of the sprite in the Tile class, and... IT WORKS! Here's the texture manager class I created.

TextureManager.h
#ifndef TEXTUREMANAGER_H
#define TEXTUREMANAGER_H

#include <SFML/Graphics.hpp>
#include <map>

class TextureManager {
        private:
                std::map<const char *, sf::Texture> textures;

        public:
                TextureManager();
                virtual ~TextureManager();

                void AddTexture(const char * filename, const char * identifier);
                sf::Texture* GetTexture(const char * identifier);
};

#endif
 

TextureManager.cpp
#include "TextureManager.h"
#include <cstdlib>

TextureManager::TextureManager() {

}

TextureManager::~TextureManager() {

}

void TextureManager::AddTexture(const char * filename, const char * identifier) {
        sf::Texture texture;
        if(!texture.loadFromFile(filename)) {
                // There was an error loading the image...
                printf("Error loading image from file: %s", filename);
        } else {
                this->textures[identifier] = texture;
        }
}

sf::Texture* TextureManager::GetTexture(const char * identifier) {
        return &(this->textures[identifier]);
}
 

And then in the Init() function of my SFMLApp class (creates the SFML window and handles the game information), I simply do this...

// Load the textures into the texture manager.
this->textureManager.AddTexture("WoodTexture01.png", "Wood01");
 

And then, to load that texture into the sprite of each tile, I simply pass a pointer to the texture to the Tile class constructor, and do this...

if(texture != NULL) {
        // Set the sprite's texture.
        this->sprite.setTexture(*texture);
        this->sprite.setPosition((this->cx - (Tile::TILE_SIZE / 2)), (this->cy - (Tile::TILE_SIZE / 2)));
        this->sprite.setScale((Tile::TILE_SIZE / texture->getSize().x), (Tile::TILE_SIZE / texture->getSize().y));
}
 

Now... maybe I can finally get on with the rest of my tile engine?

5
Graphics / Re: Integer Division by Zero Exception when Drawing Shape?
« on: November 22, 2013, 06:27:54 pm »
Using this code works, by the way, and loads the image just fine with no fuss...

Tile.h
#ifndef TILE_H
#define TILE_H

#include <SFML/Graphics.hpp>

enum TileState {
        HIDDEN,
        REVEALED,
        VISIBLE
};

class Tile {
        private:
                float cx;
                float cy;
                sf::RectangleShape shape;
                sf::Texture texture;
                //sf::Sprite sprite;

                TileState state;
        public:
                static const float TILE_SIZE;

                Tile(float x = 0.0f, float y = 0.0f, const char * filename = NULL);
                virtual ~Tile();

                void ChangeState(TileState newState);
                void Draw(sf::RenderWindow* window);
};

#endif
 

Tile.cpp
#include "Tile.h"
#include <cstdlib>

const float Tile::TILE_SIZE = 48.0f;

Tile::Tile(float x, float y, const char * filename) {
        this->cx = x;
        this->cy = y;

        this->shape = sf::RectangleShape(sf::Vector2f(Tile::TILE_SIZE, Tile::TILE_SIZE));
        this->shape.setPosition((x - (Tile::TILE_SIZE / 2)), (y - (Tile::TILE_SIZE / 2)));
        this->shape.setFillColor(sf::Color(0, 0, 0, 255));

        if(filename != NULL) {
                // Load the texture.
                if(!this->texture.loadFromFile(filename)) {
                        // There was an error loading the texture...
                        printf("Error loading file: %s", filename);
                } /*else {
                        this->sprite.setTexture(this->texture);
                        this->sprite.setPosition((this->cx - (Tile::TILE_SIZE / 2)), (this->cy - (Tile::TILE_SIZE / 2)));
                        this->sprite.setScale((Tile::TILE_SIZE / this->texture.getSize().x), (Tile::TILE_SIZE / this->texture.getSize().y));
                }*/

        }

        this->state = TileState::HIDDEN;
}

Tile::~Tile() {
       
}

void Tile::ChangeState(TileState newState) {
        this->state = newState;
        if(newState == TileState::REVEALED) this->shape.setFillColor(sf::Color(0, 0, 0, 200));
}

void Tile::Draw(sf::RenderWindow* window) {
        if(this->state != TileState::HIDDEN) {
                //if(!(&this->sprite == NULL)) window->draw(this->sprite);
                sf::Sprite sprite;
                sprite.setTexture(this->texture);
                sprite.setPosition((this->cx - (Tile::TILE_SIZE / 2)), (this->cy - (Tile::TILE_SIZE / 2)));
                sprite.setScale((Tile::TILE_SIZE / this->texture.getSize().x), (Tile::TILE_SIZE / this->texture.getSize().y));
                window->draw(sprite);
                if(this->state == TileState::REVEALED) window->draw(this->shape);
        }
}
 

So what's the huge difference that's causing the problem?

6
Graphics / Re: Integer Division by Zero Exception when Drawing Shape?
« on: November 22, 2013, 06:18:15 pm »
I have no idea what to do... and to top it off, when I try to build in release mode, it keeps telling me there are unresolved externals, which leads me to believe that it can't find the libraries... but the release libraries are in the directory I specified and I know I typed them correctly... if it can find the debug libraries, why not the release libraries? I'm using the static libraries........ I can't stand all this linker and building BS.

And looking through the vector of Tiles, some of them seem to have textures with the proper size, and some seem to have numbers specified in e+ notation...

What am I doing wrong?!

7
Graphics / Re: AW: Integer Division by Zero Exception when Drawing Shape?
« on: November 22, 2013, 05:55:35 pm »
I wasn't talking about the image, but about the texture in memory ;)

Well then, make sure you have the debug libs of SFML with debug symbols, then set a break point at the draw call and when it stops step into the function call to determine where the error is within SFML's code.
The only division I know is with the texture size. ;)

I just edited my post, but in case you don't see that, I checked further back in the call stack and the texture size for the one that's causing the crash is zero... but how? Like I said, I'm doing NOTHING different from how I was creating the sprite before with the exception of its scope being part of the Tile object, rather than being created with each draw() call.

8
Graphics / Re: Integer Division by Zero Exception when Drawing Shape?
« on: November 22, 2013, 05:47:26 pm »
Yes, I copied the texture from another folder to the folder in which it's being loaded from, but like I said, this code was working fine before and loading the image properly... I don't think the image is corrupt. I even have screenshots to prove that I was able to draw it before.

And besides, when looking at the local variables when the debugger comes up, the texture's size variable says 48 for both x and y, which is what they should be...

EDIT: Checked further back in the call stack to see which tile was causing the crash, and when I checked that Tile variable's texture, it had size of 0... so why did it say 48 when it reached the draw call? And how could it be corrupt or loading improperly? I'm doing NOTHING different from before with the exception of the scope of the Sprite variable...

9
Graphics / Re: Integer Division by Zero Exception when Drawing Shape?
« on: November 22, 2013, 05:35:17 pm »
It occurs on the line that draws the shape, which makes no sense, because there's no integer division involved in the creation or modification of the shape... none of the local variables seem to have any suspicious values. If there's something obvious I'm missing, please point it out, because I never seem to get anywhere with the debugger.

EDIT: It doesn't even seem to be the shape, either. I tried commenting out the line that draws the shape, and even tried commenting out the line that sets the scale of the sprite, but the exception still occurs. I noticed, in the few times that the window stayed open long enough, that nothing was "drawn" and I was seeing white squares. Google tells me this is indicative of a problem loading the sprite, but I'm using the same code I was using when I was loading the sprite in every draw() call, with the only difference being that instead of creating a new sprite object every call, the Sprite object is stored in the Tile object and initialized only if there is no problem loading the texture... WHAT IS WRONG?! I'm going insane...

10
Graphics / Re: Integer Division by Zero Exception when Drawing Shape?
« on: November 22, 2013, 03:47:57 pm »
Ah, I guess I've been spoiled by Java syntax. And as far as the ! operator, I knew that, but I must have been sleepier than I thought when I wrote that, because that's not the way I usually form expressions. However, I'm still getting the Integer Division by Zero exception even after I made a single constructor with default parameters. The only place I'm dividing at all is when I set the scale of the sprite, but how could the texture be blank if the sprite's information is only set if the texture is loaded properly?

Tile.h
#ifndef TILE_H
#define TILE_H

#include <SFML/Graphics.hpp>

enum TileState {
        HIDDEN,
        REVEALED,
        VISIBLE
};

class Tile {
        private:
                float cx;
                float cy;
                sf::RectangleShape shape;
                sf::Texture texture;
                sf::Sprite sprite;

                TileState state;
        public:
                static const float TILE_SIZE;

                Tile(float x = 0.0f, float y = 0.0f, const char * filename = NULL);
                virtual ~Tile();

                void ChangeState(TileState newState);
                void Draw(sf::RenderWindow* window);
};

#endif
 

Tile.cpp
#include "Tile.h"
#include <cstdlib>

const float Tile::TILE_SIZE = 48.0f;

Tile::Tile(float x, float y, const char * filename) {
        this->cx = x;
        this->cy = y;

        this->shape = sf::RectangleShape(sf::Vector2f(Tile::TILE_SIZE, Tile::TILE_SIZE));
        this->shape.setPosition((x - (Tile::TILE_SIZE / 2)), (y - (Tile::TILE_SIZE / 2)));
        this->shape.setFillColor(sf::Color(0, 0, 0, 255));

        if(filename != NULL) {
                // Load the texture.
                if(!this->texture.loadFromFile(filename)) {
                        // There was an error loading the texture...
                        printf("Error loading file: %s", filename);
                } else {
                        this->sprite.setTexture(this->texture);
                        this->sprite.setPosition((this->cx - (Tile::TILE_SIZE / 2)), (this->cy - (Tile::TILE_SIZE / 2)));
                        this->sprite.setScale((Tile::TILE_SIZE / this->texture.getSize().x), (Tile::TILE_SIZE / this->texture.getSize().y));
                }
        }

        this->state = TileState::HIDDEN;
}

Tile::~Tile() {
       
}

void Tile::ChangeState(TileState newState) {
        this->state = newState;
        if(newState == TileState::REVEALED) this->shape.setFillColor(sf::Color(0, 0, 0, 200));
}

void Tile::Draw(sf::RenderWindow* window) {
        if(this->state != TileState::HIDDEN) {
                window->draw(this->sprite);
                if(this->state == TileState::REVEALED) window->draw(this->shape);
        }
}
 

Also, when I check the variables in the debugger, the texture's size is exactly what it should be... 48... so how could I be getting division by zero?

11
Graphics / Integer Division by Zero Exception when Drawing Shape?
« on: November 22, 2013, 07:45:48 am »
I honestly have no idea what I'm doing anymore... I tried making both the texture and shape part of my class so I wouldn't have to create the shape on the fly in each draw() call, which would improve speed, but I can't seem to get it to work. At first, I thought it was the sprite itself not initializing properly once I assign the loaded texture to it, but the debugger seems to point me to the line in which I'm drawing the shape itself, rather than the line on which I draw the sprite. The shape itself hasn't given me problems before, so I don't know... maybe it IS the sprite...... can someone take a look at my class and tell me if I just done goof'd somewhere with the texture and sprite?

Tile.h
#ifndef TILE_H
#define TILE_H

#include <SFML/Graphics.hpp>

enum TileState {
        HIDDEN,
        REVEALED,
        VISIBLE
};

class Tile {
        private:
                float cx;
                float cy;
                sf::RectangleShape shape;
                sf::Texture texture;
                sf::Sprite sprite;

                TileState state;
        public:
                static const float TILE_SIZE;

                Tile();
                Tile(float x, float y);
                Tile(float x, float y, const char * filename);
                virtual ~Tile();

                void ChangeState(TileState newState);
                void Draw(sf::RenderWindow* window);
};

#endif
 

Tile.cpp
#include "Tile.h"
#include <cstdlib>

const float Tile::TILE_SIZE = 64.0f;

Tile::Tile() {
        Tile(0.0f, 0.0f);
}

Tile::Tile(float x, float y) {
        Tile(x, y, NULL);
}

Tile::Tile(float x, float y, const char * filename) {
        this->cx = x;
        this->cy = y;

        this->shape = sf::RectangleShape(sf::Vector2f(Tile::TILE_SIZE, Tile::TILE_SIZE));
        this->shape.setPosition((x - (Tile::TILE_SIZE / 2)), (y - (Tile::TILE_SIZE / 2)));
        this->shape.setFillColor(sf::Color(0, 0, 0, 255));

        if(filename != NULL) {
                // Load the texture.
                if(!this->texture.loadFromFile(filename)) {
                        // There was an error loading the texture...
                        printf("Error loading file: %s", filename);
                } else {
                        this->sprite.setTexture(this->texture);
                        this->sprite.setPosition((this->cx - (Tile::TILE_SIZE / 2)), (this->cy - (Tile::TILE_SIZE / 2)));
                        this->sprite.setScale((Tile::TILE_SIZE / this->texture.getSize().x), (Tile::TILE_SIZE / this->texture.getSize().y));
                }
        }

        this->state = TileState::HIDDEN;
}

Tile::~Tile() {
       
}

void Tile::ChangeState(TileState newState) {
        this->state = newState;
        if(newState == TileState::REVEALED) this->shape.setFillColor(sf::Color(0, 0, 0, 200));
}

void Tile::Draw(sf::RenderWindow* window) {
        if(!this->state == TileState::HIDDEN) {
                window->draw(this->sprite);
                if(this->state == TileState::REVEALED) window->draw(this->shape);
        }
}
 

I really appreciate any help, because C++ really gives me a headache sometimes. No wonder I gave it up for Java two years ago...

Pages: [1]