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

Author Topic: Problem with rendering (SFML2.1, OOP + <vector>)  (Read 3151 times)

0 Members and 1 Guest are viewing this topic.

marcin107

  • Newbie
  • *
  • Posts: 23
    • View Profile
Problem with rendering (SFML2.1, OOP + <vector>)
« on: June 24, 2014, 10:29:58 am »
Hi guys, i have a big problem which i can't solve after hours and i need your help.
My program task is to make a skin move on the map and pick up item which will go into my inventory.
The problem is that my program doesn't want to render skins properly. Take a look on the code which is long and then i'll give you more details. 
main.cpp
(click to show/hide)
Item.h
(click to show/hide)
Item.cpp
(click to show/hide)
Gracz.h
(click to show/hide)
Gracz.cpp
(click to show/hide)

So, as you can take a look at the main.cpp I declared an object of Gracz class and i gave it a skin "skin.png", in my program it's just a black square, but later i gave a skin to an item "item1.png" which was little black heart in square, but what i got is next black square! Just to satisfy my curiosity later a gave Gracz object "item1.png" skin instead of "skin.png" and everything on the map(items and Gracz object) had the same picture(little black heart on square) but with other size(Gracz object had skin 50x50 and item is 30x30)
 The next thing when i push_back my items vector the next items don't want to show up on the map. I mean they are on the map, i can pick up them, but i can't see them.
It seems like something would be rendered wrong maybe it's in vector class or my loops, don't really know.
I would appreciate your help.

UPDATE: I found out that when i'll go my Gracz black square to the "invisible item" it shows up that Item is a white square. Sometimes when i pick up "invisible item", a new one respawn with a proper skin. 
« Last Edit: June 24, 2014, 10:45:11 am by marcin107 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10820
    • View Profile
    • development blog
    • Email
Re: Problem with rendering (SFML2.1, OOP + <vector>)
« Reply #1 on: June 24, 2014, 10:53:15 am »
It's better to use the term "texture" instead of "skin", so everyone immediately understands what you're talking about. ;)

One important part of programming is debugging and debugging also includes the isolation of problems. If you have something that acts odd, either strip out everything that's not causing the odd behavior or start from scratch with as little code as possible, till you get the same issue again. By reducing the amount of code in use, you can pretty easily isolate issues.

When you set a texture to a sprite, the sprite will know where exactly the texture is located in the memory, thus if you now move the texture somewhere else, the sprite can't "find" the texture anymore, thus usually displaying as white sprite.
The power of a std::vector is that the memory is continuous, but this also means, the more objects you add, the more continuous memory is needed and as such everything has to be moved somewhere to a bigger space in the memory.
But as we've established before, the sprite knows where the texture is in memory, thus if it gets moved - which is what happens when pushing back more and more objects to a vector - the sprite gets rendered as white square.

Anyways it's advised to manage textures apart from your entities, for instance something like the ResourceHolder from the SFML Game Development book. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

marcin107

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Problem with rendering (SFML2.1, OOP + <vector>)
« Reply #2 on: June 24, 2014, 11:11:14 am »
I was trying to find out what is going wrong, i was checking the constructor, setskin() function and i couldn't find anything. I can tell you that it's my first project with <vector> library, so i didn't know the thing about moving texture in memory.

So how to use ResourceHolder to not change my classes' structures too much?
I don't know how to use enums yet, in this project i don't want to use them. I'll try to learn them during the holidays, so most probably after this project. 

Peteck

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
Re: Problem with rendering (SFML2.1, OOP + <vector>)
« Reply #3 on: June 24, 2014, 11:52:13 am »
I don't know how to use enums yet, in this project i don't want to use them. I'll try to learn them during the holidays, so most probably after this project.
I may comfort you with that it only takes about 5 minutes to learn  :D
Enum is just a way to make an integer human-readable.
« Last Edit: June 24, 2014, 12:02:21 pm by Peteck »

marcin107

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Problem with rendering (SFML2.1, OOP + <vector>)
« Reply #4 on: June 24, 2014, 12:01:24 pm »
Good than, but first i have to make this damn project work -.-
I'm trying to make an array Item items[4], but than in Gracz.cpp i have a problem with checkcollision() with passing arrays.
« Last Edit: June 24, 2014, 12:03:59 pm by marcin107 »

marcin107

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Problem with rendering (SFML2.1, OOP + <vector>)
« Reply #5 on: June 24, 2014, 12:41:09 pm »
Okey, with normal C++ array it works, but it's much harder to use it. push_back, erase are really useful things. Now i have to call constructor when i create an array
Item items[4] = {Item(1, interfejs.getRandom(15, 1265), interfejs.getRandom(15, 985)),
    Item(1, interfejs.getRandom(15, 1265), interfejs.getRandom(15, 985)),
    Item(1, interfejs.getRandom(15, 1265), interfejs.getRandom(15, 985)),
    Item(1, interfejs.getRandom(15, 1265), interfejs.getRandom(15, 985))
    };
what is really annoying.
« Last Edit: June 24, 2014, 12:49:22 pm by marcin107 »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Problem with rendering (SFML2.1, OOP + <vector>)
« Reply #6 on: June 24, 2014, 01:00:39 pm »
You should learn more about the STL containers, they're pretty important for C++.

There is std::array in C++11, which is basically a raw array with a vector-style interface, which sounds like exactly what you wish you were using.

There are also a lot of other "stable" containers (ie, containers whose individual elements will stay in the same memory location), including std::list, std::map, std::set, and the multi/unordered variants of maps and sets.

marcin107

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Problem with rendering (SFML2.1, OOP + <vector>)
« Reply #7 on: June 24, 2014, 01:48:34 pm »
@Ixrec small steps forward, holidays are pretty long and i'm going to learn some about c++. I'll take on my attention what you said, but first enums, then we will see.

For those who are "interested" in my project, it works as it should, but i have a question:
Why when i'm holding(W or D or S or A) and at the same time moving mouse, my square is getting speed boost? :D

My main loop
(click to show/hide)
« Last Edit: June 24, 2014, 01:51:53 pm by marcin107 »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Problem with rendering (SFML2.1, OOP + <vector>)
« Reply #8 on: June 24, 2014, 01:55:14 pm »
Since there's nothing mouse-related in your code (and assuming you aren't hiding the mouse stuff elsewhere), my totally uneducated guess would be that mouse movement somehow affects framerate, since your code is clearly not framerate-independent.

For the gentle newbie intro to dealing with framerate, read this: https://github.com/SFML/SFML/wiki/FAQ#graphics-smooth-animation

Of course, it could be something entirely unrelated, but we'd have to see a complete and minimal example from you to do anything besides guess.
« Last Edit: June 24, 2014, 01:57:12 pm by Ixrec »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10820
    • View Profile
    • development blog
    • Email
Re: Problem with rendering (SFML2.1, OOP + <vector>)
« Reply #9 on: June 24, 2014, 02:10:54 pm »
Moving the mouse generates a mouse move event, which will let the event loop iterate once more.
And since you're using the the sf::Keyboard class inside the event loop instead of actually handling the keyboard event - you're doing it wrong - the entity gets moved multiple times aka speed boost.

See here on how to handle events correctly: http://www.sfml-dev.org/tutorials/2.1/window-events.php
And here to see how to handle real time inputs: http://www.sfml-dev.org/tutorials/2.1/window-inputs.php
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

marcin107

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Problem with rendering (SFML2.1, OOP + <vector>)
« Reply #10 on: June 24, 2014, 02:27:40 pm »
eXpl0it3r you are right! Thanks.
It works now.

 

anything