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

Pages: [1]
1
Hi, this is basically just a compressed version of this thread: http://en.sfml-dev.org/forums/index.php?topic=15414.0

i figured that this is better suited in this section of the forum.

So in short:
Quote
i want to split my window in three (well known) parts basically:

1) game region where the game itself is drawn e.g. the map, units and everything else
2) minimap in the typical left bottom corner
3) unitmenu in the bottom right part of the screen for the player to give commands to the units

i ignored the third point for now, so im messing around with sf::View to get what i want.

i was told that to have the minimap display the whole map i just need to set its view to the respective size of the whole map in pixel. This is what i did, but the map gets displaced that way, which i do not want to happen.

see for yourself:

Quote
when i do this i get this:


the view is shifted by some offset when i set the size of it to the size of the whole tilemap.

all i did was to add one line, which does what you told me:
        sf::View mapview(app_window.getDefaultView());
        sf::View minimap(app_window.getDefaultView());
        //sf::View menu(app_window.getDefaultView());

        mapview.setViewport(sf::FloatRect(0.f,0.f,1.f,0.75f));
        minimap.setViewport(sf::FloatRect(0.f, 0.75, 0.5, 0.25));
        minimap.setSize(20*32,20*32); // my tilemap is 20 x 20 with each tile having the size of 32 x 32 pixel
 

but i don't want this behaviour, i want the minimap to be drawn at the position specified in "setViewport" (well to be precise i want the map to be drawn at the top left corner of that viewport)

what i want is this (note: minimap does not show the whole map so it is not what im trying to achieve):


my code is the following:
#include <SFML/Graphics.hpp>
#include <iostream>

sf::Texture m_tileset;
sf::VertexArray m_vertices;
void setTilemap()
{
    m_tileset.loadFromFile("Tileset.png");
    m_vertices.setPrimitiveType(sf::Quads);
    m_vertices.resize(20*20*4); // we use some dummy numbers to get a mapwidth > renderwindow_width

    for (unsigned int i = 0; i < 20; ++i)
    {
        for (unsigned int j = 0; j < 20; ++j)
        {
            //get the current tile number
            int tileNumber = 34;

            // find its position in the tileset texture
            int tu = tileNumber % (m_tileset.getSize().x / 32);
            int tv = tileNumber / (m_tileset.getSize().x / 32);

            // get a pointer to the current tile's quad
            sf::Vertex* quad = &m_vertices[(i + j * 20) * 4];

            // define its 4 corners
            quad[0].position = sf::Vector2f(i * 32, j * 32);
            quad[1].position = sf::Vector2f((i + 1) * 32, j * 32);
            quad[2].position = sf::Vector2f((i + 1) * 32, (j + 1) * 32);
            quad[3].position = sf::Vector2f(i * 32, (j + 1) * 32);

            // define its 4 texture coordinates
            quad[0].texCoords = sf::Vector2f(tu * 32, tv * 32);
            quad[1].texCoords = sf::Vector2f((tu + 1) * 32, tv * 32);
            quad[2].texCoords = sf::Vector2f((tu + 1) * 32, (tv + 1) * 32);
            quad[3].texCoords = sf::Vector2f(tu * 32, (tv + 1) * 32);
        }
    }
}



int main() {
    // Create the main SFML window
    float width = 320.f;
    float height = 420.f;
    sf::RenderWindow app_window( sf::VideoMode(width,height), "SFGUI Canvas Example", sf::Style::Titlebar | sf::Style::Close );

    // Start the game loop
    app_window.setFramerateLimit(20);
    setTilemap();
    sf::RenderStates states;
    states = &m_tileset;

    sf::View mapview(app_window.getDefaultView());
    sf::View minimap(app_window.getDefaultView());
    //sf::View menu(app_window.getDefaultView());

    mapview.setViewport(sf::FloatRect(0.f,0.f,1.f,0.75f));
    minimap.setViewport(sf::FloatRect(0.f, 0.75, 0.5, 0.25));
    minimap.setSize(20*32,20*32);




    while ( app_window.isOpen() ) {
        sf::Event event;

        while ( app_window.pollEvent( event ) ) {

            // Close window : exit
            if ( event.type == sf::Event::Closed ) {
                app_window.close();
            }
        }

        app_window.clear();
        app_window.setView(mapview);
        app_window.draw(m_vertices,states);

        app_window.setView(minimap);
        app_window.draw(m_vertices,states);


        app_window.display();
    }
    return EXIT_SUCCESS;
}

the used tileset can be found in the attachment.

why is the minimap displaced when i set its size to the mapsize? i expected (and observed) this behaviour when calling sf::View::zoom(), but not for sf::View::setSize().

Advice is greatly appreciated.

2
General / Advice on window seperation needed
« on: June 02, 2014, 01:43:02 am »
hi,

i want to split my window in three (well known) parts basically:

1) game region where the game itself is drawn e.g. the map, units and everything else
2) minimap in the typical left bottom corner
3) unitmenu in the bottom right part of the screen for the player to give commands to the units

like this:

i want to use SfGUI for the menu.

How do i best implement this with sfml? if this was QT i would just use 3 widgets and 2 layouts to get the proper ordering and positions but in SFML and SFGUI im unsure how to do it.

Any advice is greatly appreciated :)

3
Graphics / VertexArray is not drawn
« on: May 27, 2014, 12:40:09 am »
Hi,

i basically took the examples code of the tile map and copied it almost identically in my testproject.
Sadly the loaded texture is not drawn, i only see a white square instead of the tiles.

Here is my code:

Note that all needed values for the VertexArray functions have valid values (i.e. the texture was definatly loaded and is drawn just fine when i test it with a sprite, height, width etc. have their proper value).
Also this was just made ad hoc, so its pretty unclean.

 See below for the code

Apparently it is the texture that does not get bound to the vertices properly?! Because i get a white square of the expected size, which means the vertexarray is somewhat drawn, just without texture attached.

4
General discussions / Thanks guys!
« on: May 25, 2014, 06:52:00 pm »
Hi,

just wanted to say THANK YOU for the great SFML Lib and the extremly good support on the forums + superb documentation! Have to take my head off to you!
Keep it going^^ (Laurent and all the others)

5
Graphics / Drawing a Tilemap the right way
« on: December 31, 2013, 07:34:09 pm »
hi, im working on a tilemap right now, and even though it works properly it feels slow which is probably because i use the sfml functions and classes the wrong way:

So my tiles look like this:

class Tile
{
private:
        const sf::Image* _source;
        sf::IntRect _section; //! Section of the source that is drawn

public:
    Tile(void)
    :_source(nullptr),_section(sf::IntRect()) {}
        Tile(const sf::Image* source,sf::IntRect section)
        :_source(source),_section(section) {}
    const sf::Image*         source(void)  const                    { return _source; }
    const sf::IntRect&             section(void) const                    { return _section; }
    void                     setSection(sf::IntRect section) { _section = section; }
};

_section is just the part of the source image the tile is using.
First question:
Should i already use sf::Texture here for the image reference? im not manipulating the images anyway.

Now the render function:
void Engine::renderFrame()
{
        int posx,posy;
        window.clear();
        for(unsigned int y = 0; y < level.getHeight(); ++y)
        {
                for(unsigned int x = 0; x < level.getWidth(); ++x)
                {
                        const Tile& tile  = level.getTile(x,y);
                        posx              = (x * level.getTilesize());
                        posy              = (y * level.getTilesize());
            drawTile(tile,posx,posy);
                }
        }
        window.display();
}

and my drawTile function:
void Engine::drawTile(const Tile& t,int posx,int posy)
{
    if(t.source() != nullptr){
        sf::Texture tex;
        sf::Sprite spr;

        tex.loadFromImage(*(t.source()),t.section());
        spr.setTexture(tex);
        spr.setPosition(posx,posy);
        window.draw(spr);
    }
}

This is my main concern:
i read that one should use as few textures as possible, now i always create a new one with every call, this should have a terrible performance.

How would one draw a tile based map like mine? Draw all the sprites to a single texture and draw that texture to the screen afterwards?

Thanks for any help :)

6
hi,

just stumbled upon this, is there a reason there is no

sf::image(std::string filename)
{
loadImage(filename);
}

constructor in the sf::image class? Would be nice to have in my opinion.

7
Graphics / Out of Bounds World automatically NOT drawn?
« on: December 26, 2013, 01:20:05 am »
hi,

short question:

When something of my 2d World (e.g. a tilemap) is drawn and parts of it are out of the windows view are they still drawn? So do i still have to check how much to draw of my world (would be strange now that i think about it)?

I remember doing a 2d worldeditor in QT and at first i drew my whole tilemap with like 200x200 dimension, even though only like 20x20 was visible at a time which made my program rather slow.

8
Graphics / sf::image vs sf::sprite in my project
« on: January 05, 2013, 11:06:49 pm »
Hi, in my current project(a beat *em up (maker,2-d sprite based), based on an existing one) for which i will use sfml 2.0. i have to load certain filetypes, one of these contains images(crazy, i know).

Now this beat 'em up of course has to handle the players character, whose image handling i'd like to discuss.
I need to store all sprites and print them on screen on demand at a given position.
This of course in certain orders predefined by the creator of that character(in external animation files describing sequences of images(does not store the images itself)).
Furthermore should simple image/animation manipulations be possible such as rotating the current sprite(s) and, most prominent, displaying afterimage effects.

Would i copy the images' data into sf::sprites(which is fast to draw) or would i create my own class inheriting from sf::drawable?
Is sf::sprites suited for this? Because the documentation of sfml 2.0 says sf::images is fast in manipulating images, nothing alike is stated for sf::sprites.

To be more precise:
What i wished my system was like:

Animations are just a set of pointers to beforehandedly loaded images who then are drawn onto the desired position on screen.
Only problem: What if i, for example, want to rotate the image by 90 degrees? I do not want to touch the loaded sprites directly, so would i have to copy those and manipulate the copies? This would oppose my idea of animations just "pointing" to images not copying them(which would be faster)...any ideas on this one?

9
Graphics / Why is my class not drawn?
« on: July 03, 2012, 06:29:45 pm »
Hello,

i got the following class:

class abc
:public sf::drawable,public error
{
public:
//something
void draw(blabla)const;
}

as you can see the class abc inheris from sf::drawable and error. The class error does not have a draw function(and it's no class that should be drawn, it just does some errormanagement), but when i inherit from the error class, abc stops getting drawn(which it correctly is, if i do not inherit from the error class).

So what do i do to change this? Does my error class have to inherit from sf::drawable as well? oO

Hmm, guess i should just add that error class as a member instead of inheriting...

10
Graphics / A few Questions concerning sf::text(sfml 2.0)
« on: July 02, 2012, 02:27:03 pm »
Hello,

Im trying to write a simple Label class which, for now, is supposed to draw a sf::text object on the screen with an rectangle surrounding it.
Im doing that by first drawing the text at the destined position and then using the text's globalposition to get the rectangle at the proper position. This works and always creates a fitting rectangle around the text, but what i actually want is to do this the other way around:
i create the background(with a fixed size) and then make the text fit it. Setting the character size of the sf::text does not give one proper control over the actual size of the text(or so it seems) and i can not like directly set the localBounds so the only solution i see right now is to solve this by iterating over possible character size's and sf::text positions.

In short:
What i wanna do in the end is to have a label with enough space for three digits of a certain size.
But i want it in this direction:
I define the (fix)size of the background rectangle(or later an image, but thats not the problem right now) and then compute the size and positon of the sf::text to properly fit my rectangle(so that neither a 1digit number nor a 3 digit number is out of the rectangle).

Thank you very much :)

 

11
Graphics / Tutorial Code does nothing
« on: December 07, 2011, 07:39:35 pm »
Hello, i just tried to compile the first graphic package tutorial

Code: [Select]
#include <SFML/Graphics.hpp>


int main()
{
    // Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");

    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        // Clear the screen (fill it with black color)
        App.Clear();

        // Display window contents on screen
        App.Display();
    }

    return EXIT_SUCCESS;
}


When i compile and run it the console window opens but nothing happens, i then have to use the task manager to shut the console down.

Apperently I set SFML up wrong or something :/ No error message is shown sadly :/

the console window is completely empty.

12
General / Error installing SFML 1.6 using code::blocks 10.05
« on: November 24, 2011, 07:55:06 pm »
Hello, i followed this instruction to install sfml:

http://www.sfml-dev.org/tutorials/1.6/start-cb.php and used the first method of installing(the copy one).
After doing what it tells me and compiling the given example code i get the following error message:

"program cant be started due to lacking libgcc_s_dw2-1.dll on my pc.[...]"
its obvious whats the problem but i dont understand why a) this has not been discussed in the tutorial and b) where i can find this file.

Pages: [1]