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

Pages: 1 2 [3]
31
Graphics / Sprite to stay in Top Left Corner Help
« on: July 05, 2012, 09:44:47 pm »
Hey guys

Im trying to get a sprite to stay in the top left corner, now im using a view like so
mainView.setCenter(512,384);
mainView.setSize(1024,768);
 

Now the red square is the player and the player is the center so i set up my view like so
mainView.setCenter(sf::Vector2f(player.pos.x+15,player.pos.y));
 

but im having some problems, i have a image that i want to go in the top left corner.
so i try this
interBtns.pos.x = v2.x;
interBtns.pos.y = v2.y;
 
now this puts it in the bottom, but how would i work it out so it will go to the top left corner? the screen resoultion is 800 by 600

[attachment deleted by admin]

32
Graphics / sf::Sprite changing a sprite problem
« on: June 24, 2012, 04:09:32 am »
Hey guys, im just testing out the sf::Sprite, now at the moment my code uses a simple image manager to load a image, it then sets the sprite of a class, so for example
item.cpp has a public variable sf::Sprite spriteImg;
so i then call item1.spriteImg.setTexture(imgManage.get_image("imgArgh.gif"),
now this is the code I have for its update, which is called when the player presses space
      sf::IntRect temp = sprite.getTextureRect();
      pos.h = temp.height; //66
      pos.w = temp.width; //66
      sprite.setTextureRect(temp);
Now this just sets up the correct height and width.
That works all fine and well but, if i then do this for example
item1.spriteImg.setTexture(imgManage.get_image("circle.gif"),
this code then executes
      sf::IntRect temp = sprite.getTextureRect();
      pos.h = temp.height; //32
      pos.w = temp.width; //32
      sprite.setTextureRect(temp);
My circle image is set to 66 width and 66 height...

What am I doing wrong? I would try and reset the sf::Sprite but there doesn't seem to be a function.

Cheers

33
Graphics / Image manager
« on: June 17, 2012, 06:43:21 pm »
Hey guys im using the https://github.com/SFML/SFML/wiki/TutorialImageManager for my test code, now at the moment I have it in my game while loop, this works fine if I use all the loading and stuff in my main loop, but I want to make my game code abit more tidy, how can i pass the ImageManager via parameter?

Here is the ImageManager.h
//Image manager header
#ifndef gIMAGEMANAGER_H
#define gIMAGEMANAGER_H
//Includes
#include <SFML/Graphics.hpp>
//Body
class gImageManager
{
public:
        gImageManager();
        ~gImageManager();

private:
        gImageManager( const gImageManager& );
        gImageManager& operator =( const gImageManager& );

public:
        const sf::Texture& get_image( const std::string& filename );
        void delete_image( const sf::Texture& image );
        void delete_image( const std::string& filename );
        void add_resource_directory( const std::string& directory );
        void remove_resource_directory( const std::string& directory );

private:
        std::map< std::string, sf::Texture > images_;
        std::vector< std::string > resource_directories_;
};
//end body
#endif
 

ImageManager.cpp
#include <map>
#include <iostream>
#include <SFML/Graphics.hpp>
//#include <boost/filesystem.hpp>
#include "gImageManager.h"

gImageManager::gImageManager() : images_(), resource_directories_()
{
}

gImageManager::~gImageManager()
{
        images_.clear();
        resource_directories_.clear();
}

const sf::Texture& gImageManager::get_image( const std::string& filename )
{
        // Check, whether the image already exists
        for( std::map<std::string, sf::Texture>::const_iterator it = images_.begin();
                 it != images_.end();
                 ++it)
        {
                if( filename == it->first )
                {
                        std::cout << "DEBUG_MESSAGE: " << filename << " using existing image.\n";
                        return it->second;
                }
        }
       
        // The image doesen't exists. Create it and save it.
        sf::Texture image;

        // Search project's main directory
        if( image.loadFromFile( filename ) )
        {
                images_[filename] = image;
                std::cout << "DEBUG_MESSAGE: " << filename << " loading image.\n";
                return images_[filename];
        }

        // If the image has still not been found, search all registered directories
        for( std::vector< std::string >::iterator it = resource_directories_.begin();
                 it != resource_directories_.end();
                 ++it )
        {
                if( image.loadFromFile( (*it) + filename ) )
                {
                        images_[filename] = image;
                        std::cout << "DEBUG_MESSAGE: " << filename << " loading image.\n";
                        return images_[filename];
                }

        }

        std::cout << "GAME_ERROR: Image was not found. It is filled with an empty image.\n";
        images_[filename] = image;
        return images_[filename];
}

void gImageManager::delete_image( const sf::Texture& image )
{
        for( std::map<std::string, sf::Texture>::const_iterator it = images_.begin();
                 it != images_.end();
                 ++it)
        {
                if( &image == &it->second )
                {
                        images_.erase( it );
                        return;
                }
        }
}

void gImageManager::delete_image( const std::string& filename )
{
        std::map<std::string, sf::Texture>::const_iterator it = images_.find( filename );
        if( it != images_.end() )
                images_.erase( it );
}

void gImageManager::add_resource_directory( const std::string& directory )
{
        // Check whether the path already exists
        for( std::vector<std::string>::const_iterator it  = resource_directories_.begin();
                 it != resource_directories_.end();
                ++it )
        {
                // The path exists. So it isn't necessary to add id once more.
                if( directory == (*it) )
                        return;
        }

        // insert the directory
        resource_directories_.push_back( directory );
}

void gImageManager::remove_resource_directory( const std::string& directory )
{
        for( std::vector<std::string>::const_iterator it  = resource_directories_.begin();
                 it != resource_directories_.end(); )
        {
                // The path exists. So it isn't necessary to add id once more.
                if( directory == (*it) )
                        it = resource_directories_.erase( it );
                else
                        ++it;
        }
}
 

Here is my while statement, also I tried to declare one in the header, but it seems it goes out of scope once again.

void gState::update()
{
        gImageManager imgManage;

        while (rtnQuit() == false)
        {
                currentState += 1;
                //Event checker
                while(screen.pollEvent(iEvent))
                {
                        if(iEvent.type == sf::Event::Closed)
                        {
                                quit = true;
                        }
                        //Events
                        //stateEvents();
                }
                //State checker
                if(currentState == 1)
                {
                        //if(buttons.at(0).rtnState() == 1 && buttons.at(0).rtnID() == 1)
                        //{
                        //      currentState = 2;
                        //      changeState();
                        //}
                }
                //Collision
                //stateCollision();
                //Update
                //stateUpdate();
                if(fps.getElapsedTime().asMilliseconds() > frameRate)
                {
                        fps.restart();
                }
                screen.clear(sf::Color(currentState,currentState,currentState));
                screen.draw(button.sprite);
                screen.display();
                //DRAW
                //stateDraw();
        }
}
 

What i want to do is a method will be called, which is changeState and that will be given a reference, so then depending on the state the correct images can be loaded, I could do this in the while loop as i have stated, but It feels to me wrong and well not professional.

Canvas

Its ok, i declared a imgManager in the header and not any where else and it seems to work fine :) cheers

34
Graphics / Returning a image to .draw
« on: June 15, 2012, 04:56:53 pm »
Hey guys, im trying to return a sf::Image to the RenderWindow.draw function,

I have some code so in a class a sf::Image is created, and i use a function to return it like so
sf::Image gGraphic::draw()
{
        return image;
}

but when i put it into code like this

screen.draw(button.draw());

the . between screen and draw is red, and displays this error
Error: no instance of overloaded function "sf::RenderWindow::draw"" matches the argument list

now I have looked at the tutorials for 1.6 (im using 2.0 but there is no image tutorial for that) and to draw a sprite you just need to return the image..so how come mind isnt working?

Canvas

I have changed my code and this is what i have now

in gButton.cpp
gButton::gButton(int x,int y,int w,int h,int ident)
{
        state = 0;
        xPos = x;
        yPos = y;
        width = w;
        height = h;
        id = ident;
        graphic.loadFromFile("images/newgamebtn.png");
        img.setTexture(graphic);
}

sf::Sprite gButton::draw()
{
        return img;
}
 

and in my gState i have
screen.draw(button.draw());
 

when i compile it now, it runs but before it loads up the window i get this error
Unhandled exception at 0x5d972382 in Phor_Temp1_SFML.exe: 0xC0000094: Integer division by zero.
then it closes, any idea? i just want to be able to return a sprite from another class

I just placed this code

sf::Sprite temp = button.draw();
                screen.clear(sf::Color(currentState,currentState,currentState));
                screen.draw(temp);
 
and i get a white box the same size of the image in the top left corner, but no image..., Do i have to pass back the image + sprite, then make them in my main loop? There must be a better way as im going to be loading alot of sprites into my game

more code has been changed, here is my gButton.h
//Player header
#ifndef gBUTTON_H
#define gBUTTON_H
//includes
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
//Using namespace

//body
class gButton{
private:
        int id;
        int state;
        float xPos, yPos;
        int width, height;
public:
        //Variables
        sf::Texture graphic;
        sf::Sprite img;

        gButton();
        gButton(int x,int y,int w,int h, int ident);
        sf::Sprite draw();
        void handleEvents(sf::Event iEvent);
        int rtnState(){return state;}
        int rtnID(){return id;}
};
//end body
#endif

and my gButton.cpp
#include "gButton.h"

gButton::gButton()
{
        //nothing
}

gButton::gButton(int x,int y,int w,int h,int ident)
{
        state = 0;
        xPos = x;
        yPos = y;
        width = w;
        height = h;
        id = ident;
        graphic.loadFromFile("images/newgamebtn.png");
        img.setTexture(graphic);
}

sf::Sprite gButton::draw()
{
        return img;
}
 

now here is my gState update method
void gState::update()
{
        while (rtnQuit() == false)
        {
                currentState += 1;
                //Event checker
                while(screen.pollEvent(iEvent))
                {
                        if(iEvent.type == sf::Event::Closed)
                        {
                                quit = true;
                        }
                        //Events
                        //stateEvents();
                }
                //State checker
                if(currentState == 1)
                {
                        //if(buttons.at(0).rtnState() == 1 && buttons.at(0).rtnID() == 1)
                        //{
                        //      currentState = 2;
                        //      changeState();
                        //}
                }
                //Collision
                //stateCollision();
                //Update
                //stateUpdate();
                if(fps.getElapsedTime().asMilliseconds() > frameRate)
                {
                        fps.restart();
                }
                screen.clear(sf::Color(currentState,currentState,currentState));
                screen.draw(button.img);
                screen.display();
                //DRAW
                //stateDraw();
        }
}
 

when i compile i get a white box and still not image...
Now i have read around, and apparently my code will set the texture up to the sprite, but then it will delete the texture because it comes out of scope, but in every forums i find no one every explains how to actually fix that, anyone have a idea?

Canvas

...

Just so you know I have just followed this tutorial https://github.com/SFML/SFML/wiki/TutorialImageManager and changed all the sf::Image's to sf::Texture's and it still comes up as a white box :(

35
General / Use setFramerateLimit or not?
« on: June 15, 2012, 04:01:13 pm »
Hey guys just a quick question, in my code im telling my RenderWindow to have this set
screen.setFramerateLimit(60);
now when i run the program, i have a int going up by 1 every frame, it goes to 255 and then goes back, now if i run the program the window goes from black to white in 8 seconds...this is wrong, 255 / 60 = 4, so it should take the window 4 seconds to update, not 8, if i set it to 120 it works fine, but im alittle bit nervous running a game at 120 FPS, does anyone have any small piece of code i can use that uses the clock to update the window instead of the setFramerateLimit

Cheers

Canvas

I now have this

framtRate 60.0f/1000.0f
if(fps.getElapsedTime().asMilliseconds() > frameRate)
{
        fps.restart();
        screen.clear(sf::Color(currentState,currentState,currentState));
}
screen.display();
but the screen update is way to fast

its ok i had screen.setVerticalSyncEnabled(false); i now have screen.setVerticalSyncEnabled(true); :) post can be deleted (sorry about my mass of topics to be deleted)

36
General discussions / Visual Studio 2010 release with SFML?
« on: June 15, 2012, 02:47:16 am »
Hey guys just a quick question, I downloaded SFML today and converted the 2008 SFML to the 2010 visual SFML, so i have them working (I hope), now i just got a blank window up. I know these is quick but how can i get my release to compile? its displaying
LINK : fatal error LNK1181: cannot open input file 'sfml-system-s.lib'
but I dont have that :D, also im asking this so early because I have a project i was working on with SDL, then i came up to the problem of smooth rotation, so i found this awesome library of SFML, now tomorrow I just gotta convert all my SDL to SFML :( but should be ok, also is SFML as supported at SDL? im planning to release a game.

Canvas

P.S. i followed this is that correct? (Do i need to convert builds from 2008 to 2010?)

Pages: 1 2 [3]