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.


Topics - packetpirate

Pages: [1]
1
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?

2
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]