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

Pages: [1] 2
1
General / [SOLVED] need a better collision idea
« on: July 11, 2020, 04:54:41 pm »
hello!
I'm creating this city like game, but having some trouble with collision between the cursor and the building tiles.
my first idea (which is working now) was to detect collision with only the ground area of each building. but its extremely counter intuitive. video explaining:
http://www.youtube.com/watch?v=6bubui54T_c

by 7 seconds I move the cursor over a building, but the selected tile is the ground behind it.

I need some insight on how to make a better collision detection, considering that buildings have different heights (and street tiles have almost no height at all).
observation: the tiles are made of sf::Quads (shaped as parallelograms/diamonds, NOT rectangles) in a sf::VertexArray.

original tile image:


tiles boundings before rotating the view (notice that most buildings won't use all the tile size).


I don't know if it is of any help, but the building tiles before rotating the view:


and after rotating:


grid lines are for reference.
any idea is apreciated. thanks in advance!  ;D

2
Graphics / strange behaviour with View and Viewport
« on: October 25, 2019, 02:52:03 pm »
hello
I'm having trouble understanding this Views and Viewport situation:
in this code, the text appears while the mouse cursor is inside view_one, which occupies 100% of the height and 80% of the width (because the Viewport is (0, 0, 0,8, 1)):
#include <SFML/Graphics.hpp>

int main(){
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Views");
    sf::View view_one, view_two;
    view_one.setViewport(sf::FloatRect(0, 0, 0.8, 1));
    view_two.setViewport(sf::FloatRect(0.8, 0, 0.2, 1));
    sf::Font font;
    font.loadFromFile("LondrinaSolid-Regular.otf");
    sf::Text text("Cursor is inside View One", font);
    while (window.isOpen()){
        sf::Event event;
        while (window.pollEvent(event)){
            if(event.type == sf::Event::Closed) window.close();
        }
        sf::Vector2f pos_in_view = window.mapPixelToCoords(sf::Mouse::getPosition(window), view_one);
        window.clear();
        if (sf::IntRect(view_one.getCenter().x-view_one.getSize().x/2, view_one.getCenter().y-view_one.getSize().y/2, view_one.getCenter().x+view_one.getSize().x/2, view_one.getCenter().y+view_one.getSize().y/2).contains(sf::Vector2i(pos_in_view))){
            window.setView(view_one);
            window.draw(text);
        }
        window.display();
    }
    return 0;
}


it works. but if I resize the view_one,
view_one.setSize(3000, 3000);
the viewport seems to be resized too




is that supposed to happen? how do I keep the correct proportions, so I can always check if the cursor is inside a given sf::View?

EDIT: I know that the VIEW should be resized, and with it the text inside. but the Viewport should not, I think. notice how the viewport shrinks to almost half the window size in the second image

EDIT 2: it works if I use
view_one.reset(sf::FloatRect(0, 0, 3000, 3000));
is this a bug? I checked the source, and apart from resizing the view (like setSize() does), reset() just recalculates the view center and set the rotation to 0;

3
Graphics / sf::text not handling characters correctly
« on: October 05, 2019, 07:58:24 pm »
hello
I'm having trouble drawing non ASCII character. i searched around, but did not found a good solution.
in the SFML window, I have this:


but when I print the same string to the console, it works:


inside the debugger the word is also correct, what makes me think its a problem in SFML interpretation of the string?


I have read the text tutorial, but I cant understand why just the SFML window is not showing the string correctly.
I tried many font types, of course all of them have the desider characters.
i'm using Linux, by the way.

this is the code:
#include <string>
#include <iostream>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/Event.hpp>
#include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/Text.hpp>

int main(){
    std::string str = "Opção olé";
    sf::Font font;
    font.loadFromFile("graphics/LondrinaSolid-Regular.otf");
    sf::Text text(str, font);
    sf::RenderWindow window(sf::VideoMode(400, 300), "SFML Text");
    std::cout << str;

    while (window.isOpen()){
        sf::Event event;
        while (window.pollEvent(event)){
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear();
        window.draw(text);
        window.display();
    }
    return 0;
}

I also tried changing the sf::Text creation line to
sf::Text text(sf::String(str), font);
but I have the same results.

do I have to change every string in my code to wstring? that would be a pain in the neck, mainly because too many functions take just strings as arguments  :-\

4
General / (almost) binary space partitioning example
« on: September 23, 2019, 04:38:02 pm »
hello everybody
I created this small example of BSP... its not done yet, and still a little buggy. but I wanted to share and ask for opinions
i'm planning to use it in some kind of random city creator. you press spacebar and it starts partitioning:



and here goes the code:
(I know most people don't like globals, but its just for testing a prototype...)
(click to show/hide)


=====EDIT=====

changed the code a bit
still produces some weirdness, but its much better. now it have streets  ;D
its kind of messy, tough. not intended for real life usage like it is, i guess.



(click to show/hide)

5
Graphics / reset Transform?
« on: July 25, 2018, 02:14:31 pm »
hello everybody

how can I reset a transform? i noticed that if i use
state.transform.translate =  sf::Vector2f(10, 0);
twice, the final position will be (20, 0). i know that it is expected, but, how can i make it to be always (10, 0)?

what i'm trying to do is move some vertexArrays to specific positions on the screen, so i can't just combine the "translate" values.

thanks in advance :D

6
Graphics / positioning sf::Text
« on: May 14, 2018, 01:16:27 pm »
hello everybody
i want to align a text box with the bottom edge of the screen. but the way i'm doing it is not working correctly. this is a minimal code example:

#include <SFML/Graphics.hpp>

int main(){
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
    sf::Font font;
    font.loadFromFile("automati.ttf");
    sf::Text text("One\nTwo\nThree\nFour", font, 30);
    text.setPosition(window.getSize().x*0.02, window.getSize().y - text.getGlobalBounds().height);
    text.setColor(sf::Color::Black);

    while (window.isOpen()){
        sf::Event event;
        while (window.pollEvent(event)){
            if (event.type == sf::Event::Closed){
                window.close();
            }
        }
        window.clear(sf::Color::White);
        window.draw(text);
        window.display();
    }

    return 0;
}

and this is the result:



i searched it, but haven't found anything about...
i'm using SFML 2.4 from the oficial Debian Testing repositories.

thanks in advance :D

7
General / "infinite" tilemap - please rate & help improve
« on: November 10, 2016, 03:07:33 am »
hello everybody.
i created this little snippet of a seamless "infinite" tilemap. it works, but i feel like it could be better. so i'm taking suggestions for everything, even variable names.

how it works:
1- check if player has moved from one tile to another
2- if yes, then search in a binary file for the current player tile
3- load player tile and some tiles around it, just enough to fill a little more than the screen
4- draw the tiles

the code is below (<100 lines). tileset is attached.
(click to show/hide)

and this is the code that generates the binary file that stores the tiles:
(click to show/hide)

I noticed that if you start at some position *relatively close* to negative values (ie 10, 10), it doesn't work as expected. i don't exactly know how to fix it.

let me know if i forgot to explain something. besides that, i take any suggestions. i really want to improve this.
thanks in advance :)

8
General / a better way to select options from menu?
« on: August 30, 2014, 06:34:49 pm »
hi,
i'd like some suggestions on how to better manage the cursor position on a menu.
for now, i have a enum that holds all options, like
enum CursorPosition {NEW_GAME, LOAD_GAME, OPTIONS, EXIT);
when the user press DownArrow, the CursorPosition goes from NEW_GAME to LOAD_GAME. if he press it again, the cursor goes to OPTIONS, etc. as its not possible to directly iterate trough enums, i use static_cast. but i feel it's too messy.
here is an example.

#include <SFML/Graphics.hpp>
enum Cursor{FIRST, SECOND, THIRD, FOURTH};

int main(){
    sf::RenderWindow window(sf::VideoMode(600, 480), "SFML window");
    const size_t num_options = 3;
    sf::RectangleShape rect[num_options];
    for (size_t n=0; n<num_options; n++){
        rect[n].setPosition(20, n*100+20);
        rect[n].setSize(sf::Vector2f(560, 80));
    }
    Cursor cursor = FIRST;
    while (window.isOpen()){
        sf::Event event;
        while (window.pollEvent(event)){
            if (event.type == sf::Event::Closed){
                window.close();
            }

            //////////////////////////////////////////////
            //Here is where I handle the cursor movement//
            else if (event.type == sf::Event::KeyPressed){
                int c = static_cast<int>(cursor);
                switch(event.key.code){
                    case sf::Keyboard::Up:{
                        c--;
                        if (c<0){c=0;}
                        break;
                    }
                    case sf::Keyboard::Down:{
                        c++;
                        if (c>=num_options){c=num_options-1;}
                        break;
                    }
                    default:{
                        break;
                    }
                }
            cursor = static_cast<Cursor>(c);
            }
        }
        window.clear(sf::Color::White);
        for (size_t n=0; n<num_options; n++){
            rect[n].setFillColor(sf::Color::Green);
            if (cursor == static_cast<Cursor>(n)){
                rect[n].setFillColor(sf::Color::Red);
            }
            window.draw(rect[n]);
        }
        window.display();
    }
    return 0;
}

is that a good approach? or is there some better way to make the cursor move on a menu? how do people usually do that?
sorry for this being more a design-related question  :P

thanks in advance!

9
General / opinions: how to handle resource errors?
« on: November 04, 2013, 06:48:29 pm »
hello,

i was thinking, how do people usually handle loading erros in SFML?
i mean, if you make a graphical application, without the console running on background, and it fails to load a font: how are you going to say to the user "hey, i had a problem loading fonts"?

because if there is no terminal, you can't output errors to it; and if there's no sf::Font, you can't show a sf::Text on the screen. so, what would you do?

thanks for the suggestions :)

10
Graphics / Transparent color and RenderTexture
« on: October 22, 2013, 02:56:23 am »
hello everybody. consider this little piece of code:

#include <SFML/Graphics.hpp>
int main(){
    sf::RenderTexture temp_render;
    sf::Texture tex;
    sf::Sprite spr;
    temp_render.create(200, 200);
    temp_render.clear(sf::Color::Transparent);

    sf::VertexArray square;
    sf::Color color_1(255, 0, 0, 255);
    sf::Color color_2(255, 0, 0, 0);
    square.setPrimitiveType(sf::PrimitiveType::Quads);
    square.append(sf::Vertex(sf::Vector2f(0, 0), color_2));
    square.append(sf::Vertex(sf::Vector2f(0, 200), color_2));
    square.append(sf::Vertex(sf::Vector2f(200, 200), color_1));
    square.append(sf::Vertex(sf::Vector2f(200, 0), color_1));

    temp_render.draw(square);
    temp_render.display();
    tex = temp_render.getTexture();
    spr.setTexture(tex);

    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML Window");

    while (window.isOpen()){
        sf::Event event;
        while (window.pollEvent(event)){
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear(sf::Color::White);
        window.draw(spr);
        window.display();
    }
    return 0;
}

if you render it, you'll get:


BUT, if you change the line
window.draw(spr);
to
window.draw(square);
meaning that you'll draw the square directly to the screen, instead of drawing it to a RenderTexture and then to the screen, you'll get:


what makes me think: why this difference? I believe that when setting something to sf::Color::Transparent, it stores black pixels (as sf::Color::Transparent = sf::Color(0, 0, 0, 0)). but these pixels shouldn't be shown at all, since they're transparent, right?
as i don't know if this is expected or is a bug, i tought it would be better to ask here :)
thanks!

11
Graphics / how to properly to use this shader?
« on: October 03, 2013, 06:26:31 pm »
hello everybody. i'm trying to create an outline effect for texts in SFML. since it's not natively possible, i'm trying to use a shader for that, which is found here.

so, i pasted it in a file called outline.shader, and load it in the code like this:
if(sf::Shader::isAvailable()){
    std::cout << "Shaders are avaliable\n";
    text_outline.loadFromFile("images/fonts/outline.shader", sf::Shader::Fragment);
    text_outline.setParameter("outlineColor", sf::Color::White);
    text_outline.setParameter("texSize", sf::Vector2f(2.0, 2.0));
}
[...]
game_window.draw(title_text, &text_outline);
 

and theeen, i get this completely weird result:



i don't have any experience with shaders, so i barely understand what's happening for this strange result shows up.
so, could anyone help me solve this?

thanks in advance!

12
General / designing seamless worlds?
« on: September 21, 2013, 08:53:01 pm »
hello everybody
today i'd like to ask how would you design a tiled seamless world?

i tried to create one, and so far it's working good.
i have a sf::View, and a slighter greater sf::Rect around it, so I only draw tiles that are inside the Rect before updating the View and drawing it to the screen. something like this:

void GameMap::drawMap(GameSys &game_sys_p){
    //get the drawArea (sf::Rect around the View)
    int mapX_min = game_sys_p.getDrawArea().left;
    int mapX_max = game_sys_p.getDrawArea().width;
    int mapY_min = game_sys_p.getDrawArea().top;
    int mapY_max = game_sys_p.getDrawArea().height;

    //search tiles inside the drawArea
    for (int mapX=mapX_min/32; mapX<mapX_max/32; mapX++){
        for (int mapY=mapY_min/32; mapY<mapY_max/32; mapY++){
            if (tiles_map[mapX][mapY].getType() == 1){
                sprite1.setPosition(mapX*32, mapY*32);
                game_sys_p.getGameWindow().draw(sprite1);
            }
        }
    }
}

but now i have to put characters in it. and this is a problem, since characters can walk almost on every pixel, and the tiles are drawn only at every 32 pixels. the only way i see to solve it is cycling trough ALL game characters and objects and checking it's positions every frame, to know if they are or not inside the drawArea. but i guess that it will make things too much slow. so i'm here asking the pros for any tips on that: how to optimize a seamless world?  :P
thanks in advance!

13
Graphics / how to subtract colors?
« on: September 15, 2013, 02:04:11 am »
hello everybody
i have an opaque object, and now i want to lessen it's alpha value.
there is no overload to the '-' operator, and trying something like
text_box.setFillColor(text_box.getFillColor() + sf::Color(0, 0, 0, -15));
doesn't work.

so, how can i subtract 15 from the alpha value? this is in a loop, to be subtracted by this amount many times.
thanks in advance!

14
Window / moving sf::View from sf::RenderWindow
« on: September 09, 2013, 06:54:46 pm »
hello everybody
is there any way to directly move the view copy stored in the window object?
sf::RenderWindow window;
sf::View view;

view.setCenter(sf::Vector2f(100, 0));
view.setSize(400, 400);
window.create(sf::VideoMode(400,400), "Test!");
window.setView(view);
 

according to this topic, i should call this line every time the view is changed, but it made my cpu usage go from less than 1% to about 48%
window.setView(view);

so, isn't there any way to directly move the view copy stored in the window object?
thanks in advance!

15
Window / emptying sf::Window from events?
« on: September 07, 2013, 10:57:23 pm »
hello everybody
i made a simple intro screen, where it fades in a image, waits for the user to press anything in the keyboard, and then fades out the image.

the problem is that if the user press any button before the image is completely showing in the screen,  it fades in and out directly (instead of fading in and only then accepting user inputs)

this is the code (game_sys_p is an object that holds the sf::Window; and intro1_spr is a sf::Sprite)

sf::Event intro_events;
    int fade_factor = 1;
    for (int i=0; i>=0; i+=5*fade_factor){
        game_sys_p.getGameWindow().clear(sf::Color(0, 0, 0, i));
        intro1_spr.setColor(sf::Color(255, 255, 255, i));
        game_sys_p.getGameWindow().draw(intro1_spr);
        game_sys_p.getGameWindow().display();
        if (i>=255){
            fade_factor=-1;
                while (intro_events.type != sf::Event::KeyPressed){
                    game_sys_p.getGameWindow().pollEvent(intro_events);
                    sf::sleep(sf::milliseconds(10));
                }
        }
        sf::sleep(sf::milliseconds(20));
    }

i believe this happens because sf::Event is queueing the events (and i think it is expected). is there any way to prevent that?
or any change i could do to the code to fix it? any suggestion is welcome :)

thanks in advance!

Pages: [1] 2
anything