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

Author Topic: Drawing Everything  (Read 29798 times)

0 Members and 1 Guest are viewing this topic.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Drawing Everything
« Reply #15 on: May 18, 2008, 04:43:04 pm »
Quote
At least there should be an overloaded function which would accept both iterators and index integers.

It's much more flexible and maintainable to provide one set of functions that work with an iterator, plus a simple way to convert an index to an iterator, rather than duplicating every function. And iterators are the generic way of working with standard containers, indexes are just a specific access for vectors and strings.

Quote
But do you think it's better to use iterators all the time (even as loop-counting variables)?

Of course it is ! :)
There's really no advantage to use indices where iterators could be used.

Quote
Because at the moment I try to avoid them wherever possible

Why ? That's definitively a bad habit.

Quote
Might it be significant for the performance if I got big sf::Sprite vectors which are drawn every frame?

Using iterators or indexes will hardly make any difference in your program, especially when we talk about real-time graphics... That's nanoseconds (for container access) versus milliseconds (for displaying sprites).
Or, if you prefer, thats an integer multiplication + addition versus setting render states, sending geometry through AGP or PCI express, and going through the whole graphics pipeline (transform, lighting, clipping rasterization, texturing, blending, ...).
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Drawing Everything
« Reply #16 on: May 18, 2008, 05:16:04 pm »
So the argument for iterators is the flexibility and not the performance?
Therefore I can leave my lovely indexes for the next time ;)  - no I think I will change my code.

Quote from: "Laurent"
Quote
Because at the moment I try to avoid them wherever possible

Why ? That's definitively a bad habit.

I don't have a lot of experience with iterators, and I don't need the containers too intensely. I only use std::vector<> and std::string, and until now, everything worked well with indexes, so I didn't see any reason to use iterators.

But I didn't know about the iterator's advantages (and to tell the truth, still don't know everything), I thought it would be just a formal matter. Thank you for the declarations. I guess I will adapt my code. But what is now the point of using iterators? Wouldn't it be okay to use indexes in future? Are there some long-dated consequences I don't recognize yet?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Redien

  • Newbie
  • *
  • Posts: 30
    • View Profile
Drawing Everything
« Reply #17 on: May 18, 2008, 05:49:27 pm »
What if you want to change one of those vectors to a list in the future? Then your code would break and you would have to spend time rewriting parts of it.

workmad3

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
Drawing Everything
« Reply #18 on: May 18, 2008, 07:04:18 pm »
With a vector, I think indicies may be slightly faster, but are less flexible (you can only use them with vectors, deques and pointer-arrays) whereas iterator semantics can keep the same codewith any container, as long as you only use forward-iterator features (this is all you need to step sequentially through every element in a container, so pretty much all you need). The better method is to use structures that remove the need to worry about iterators in any way, shape or form though, such as with boost FOR_EACH, or writing a map-algorithm that can take a function (or functor) that will apply a function to each entry in a container. C++ 0x should contain much easier ways of doing this as well (such as an improved for_each construct and lambda functions :D) but that is still in the future. Performance wise though, indicies will be pretty much the same or worse than iterators when using anything other than a vector (and even then, iterators will be so close to index access that it makes no real difference. After all, a vector iterator is a really easy construct :))

Kernelpanic

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
    • http://eisenholz.bplaced.net
Drawing Everything
« Reply #19 on: May 18, 2008, 08:47:36 pm »
Why should they be faster in vectors and deques?
The adress must be evaluated, it is only less difficult than in a list.

qsik

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Drawing Everything
« Reply #20 on: May 18, 2008, 11:30:27 pm »
i cant get the last image to appear...what do i do!?

Code: [Select]
int x = 0;
int y = 144;

void SetPositions()
{
for (int repeat = 0; repeat < 26; repeat++)
{
int Position = 1;
Questions[Position].SetPosition(x,y);
Position = Position+1;
x = x=143;
if (x > 600)
y = y+1;

};
}


this also produces strange results

Redien

  • Newbie
  • *
  • Posts: 30
    • View Profile
Drawing Everything
« Reply #21 on: May 19, 2008, 03:01:45 am »
Code: [Select]
int Position = 1;
First of all, you're declaring the integer Position in the for-loop. So the code will only access the question at index 1. I'm guessing you want to loop through all the questions. Why don't you just use repeat instead of creating a new counter variable?

Code: [Select]
x = x=143;
I'm not sure what you want to do with this line, but it will produce the same code as
Code: [Select]
x = 143;

qsik

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Drawing Everything
« Reply #22 on: May 19, 2008, 06:14:33 am »
the above code was riddled with typos oops XD

when debugging, im getting lots of access violations?

qsik

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Drawing Everything
« Reply #23 on: May 19, 2008, 06:54:53 am »
sorry for double posting but the SVN just broke my code

Code: [Select]
#ifdef _DEBUG
#pragma comment(lib, "sfml-main-d.lib")
#pragma comment(lib, "sfml-system-s-d.lib")
#pragma comment(lib, "sfml-window-s-d.lib")
#pragma comment(lib, "sfml-graphics-s-d.lib")
#pragma comment(lib, "sfml-audio-s-d.lib")
#pragma comment(lib, "sfml-network-s-d.lib")
#endif
#ifdef NDEBUG
#pragma comment(lib, "sfml-main.lib")
#pragma comment(lib, "sfml-window-s.lib")
#pragma comment(lib, "sfml-system-s.lib")
#pragma comment(lib, "sfml-graphics-s.lib")
#pragma comment(lib, "sfml-audio-s.lib")
#pragma comment(lib, "sfml-network-s.lib")
#endif

#include "string"
#include "SFML/System.hpp"
#include "SFML/Window.hpp"
#include "SFML/Audio.hpp"
#include "SFML/Network.hpp"
#include "SFML/Graphics.hpp"


Code: [Select]
#include "SFML/Quickstart.h"

int Question = 0;
int JeopardyPos = 0;

int Jeopardy[25];
sf::Sprite Questions[25];
sf::Sprite HorizontalBars[5];
sf::Sprite VerticalBars[5];
sf::Sprite Categories[5];

sf::Image n100;
sf::Image n200;
sf::Image n300;
sf::Image n400;
sf::Image n500;

sf::Image Who;
sf::Image Dates;
sf::Image Where;
sf::Image Numbers;
sf::Image Random;

sf::Image HorizontalBar;
sf::Image VerticalBar;

int LoadImages()
{
if (!Who.LoadFromFile("G:/Jeopardy/Jeopardy Who.bmp"))
return EXIT_FAILURE;

if (!Dates.LoadFromFile("G:/Jeopardy/Jeopardy Dates.bmp"))
return EXIT_FAILURE;

if (!Where.LoadFromFile("G:/Jeopardy/Jeopardy Locations.bmp"))
return EXIT_FAILURE;

if (!Numbers.LoadFromFile("G:/Jeopardy/Jeopardy Numbers.bmp"))
return EXIT_FAILURE;

if (!Random.LoadFromFile("G:/Jeopardy/Jeopardy Random.bmp"))
return EXIT_FAILURE;

if (!n100.LoadFromFile("G:/Jeopardy/$100.bmp"))
return EXIT_FAILURE;

if (!n200.LoadFromFile("G:/Jeopardy/$200.bmp"))
return EXIT_FAILURE;

if (!n300.LoadFromFile("G:/Jeopardy/$300.bmp"))
return EXIT_FAILURE;

if (!n400.LoadFromFile("G:/Jeopardy/$400.bmp"))
return EXIT_FAILURE;

if (!n500.LoadFromFile("G:/Jeopardy/$500.bmp"))
return EXIT_FAILURE;

if (!VerticalBar.LoadFromFile("G:/Jeopardy/Jeopardy Vertical Bar.bmp"))
return EXIT_FAILURE;

if (!HorizontalBar.LoadFromFile("G:/Jeopardy/Jeopardy Horizontal Bar.bmp"))
return EXIT_FAILURE;

return EXIT_SUCCESS;
}

sf::Music Music;

int LoadMusic()
{
if (!Music.OpenFromFile("G:/Jeopardy/Jeopardy.wav"))
{
return EXIT_FAILURE;
};

return EXIT_SUCCESS;
}

void JeopardyArray()
{
for (int pos = 0; pos < 25; pos++)
{
Jeopardy[pos] = 1;
}
}

void QuestionsArray()
{
for (int sprite = 0; sprite < 25; sprite++)
{
if (sprite < 5)
Questions[sprite] = sf::Sprite(n100);
if (sprite < 10 && sprite > 4)
Questions[sprite] = sf::Sprite(n200);
if (sprite < 15 && sprite > 9)
Questions[sprite] = sf::Sprite(n300);
if (sprite < 20 && sprite > 14)
Questions[sprite] = sf::Sprite(n400);
if (sprite < 25 && sprite > 19)
Questions[sprite] = sf::Sprite(n500);
}
}

void BarArray()
{
for (int sprite = 0; sprite < 5; sprite++)
{
VerticalBars[sprite] = sf::Sprite(VerticalBar);
HorizontalBars[sprite] = sf::Sprite(HorizontalBar);
};
}

void SetPositions()
{
int x = 0;
int y = 148;

for (int repeat = 0; repeat < 25; repeat++)
{

Questions[repeat].SetPosition(x,y);
x = x+204;

if (x == 1020)
{
y = y+124;
x = 0;
};
};
}

int Load()
{
LoadImages();
LoadMusic();
Music.Play();
JeopardyArray();
QuestionsArray();
BarArray();
SetPositions();

return EXIT_SUCCESS;
}


Code: [Select]
#include "Headers.h"

int main()
{
sf::RenderWindow Window(sf::VideoMode(1024, 768, 32), "Jeopardy!", sf::Style::Fullscreen);
sf::Color Background(222, 41, 16);
Window.SetBackgroundColor(Background);
Load();
sf::Sprite C1(Who);
sf::Sprite C2(Dates);
sf::Sprite C3(Where);
sf::Sprite C4(Numbers);
sf::Sprite C5(Random);
C2.SetPosition(204,0);
C3.SetPosition(408,0);
C4.SetPosition(612,0);
C5.SetPosition(816,0);

bool Running = true;
while(Running)
    {
sf::Event Event;
const sf::Input& Input = Window.GetInput();
int MouseX = Input.GetMouseX();
int MouseY = Input.GetMouseY();
       
while (Window.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
                Running = false;
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
Running = false;
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::F))
Music.Play();

if ((0 < MouseX && MouseX < 162 && 124 < MouseY && MouseY < 260) && (Event.MouseButton.Button == sf::Mouse::Left) && (Jeopardy[1] == 1))
{
Question = 1;
JeopardyPos = 1;

};

if ((Event.MouseButton.Button == sf::Mouse::Right) && (Question > 0))
{
Jeopardy[JeopardyPos] = 0;
Question = 0;
};
        };

for (int x = 0;  x < 25; x++)
{
Window.Draw(Questions[x]);
};
       
Window.Display();
    }
    return 0;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Drawing Everything
« Reply #24 on: May 19, 2008, 09:57:26 am »
Are we supposed to guess the errors ?
Laurent Gomila - SFML developer

qsik

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Drawing Everything
« Reply #25 on: May 19, 2008, 11:33:33 pm »
the executable file doesnt load at all, debugging the application leaves me with lots of access violation errors?

Redien

  • Newbie
  • *
  • Posts: 30
    • View Profile
Drawing Everything
« Reply #26 on: May 19, 2008, 11:49:54 pm »
The reason you get access violations is because your for-statements try to access array elements that are outside the array boundries.

Read up on C++ arrays and try again.
http://www.cplusplus.com/doc/tutorial/arrays.html

qsik

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Drawing Everything
« Reply #27 on: May 20, 2008, 05:32:18 am »
i updated the above code and it should work yes? but it still doesn't run

quasius

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Drawing Everything
« Reply #28 on: May 20, 2008, 07:58:42 am »
Quote from: "qsik"
i updated the above code and it should work yes? but it still doesn't run


That's not enough information for anyone to help you.  Where and how does it crash?  Did you place breakpoints and step through the code to see where and why it crashes?
You need to do a bit more besides posting your entire project and asking "why doesn't it work?"

Edit:  What compiler are you using?  Do you know how to use breakpoints?  If not, you should google breakpoints for your compiler.  They are a very powerful debugging tool.

qsik

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Drawing Everything
« Reply #29 on: May 21, 2008, 01:40:52 am »
VC 2008 express edition

 

anything