Replacing vectors for something faster

I was wondering if i could use something faster than a vector.
This program creates windows and when they are being moved, they seem a little choppy
Is there anything else i could use? Any other optimizations, suggestion, anything?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
//  WindowManager.cpp
//  ResourceGatherer
//
//  Created by Angel on 10/12/12.
//  Copyright (c) 2012 Duos. All rights reserved.
//
//  Magic numbers ftw

#include "WindowManager.h"


void DrawButtons(vector<Window>::iterator it, ALLEGRO_FONT *font);


int WindowManager::AddWindow(int i_X, int i_Y, int i_Width, int i_Height, string i_Title, bool i_Master) //Adds a window to m_Windows vector and increments m_Curr ( used for ID )
{
    int temp;
    m_Windows.push_back(Window(i_X, i_Y, i_Width, i_Height, m_Curr, i_Title, i_Master));
    temp = m_Curr;
    ++m_Curr;
    return temp;
}

int WindowManager::GetWindow(int i_X, int i_Y) //Returns -2 if a window is erased, ID of windows selected or -1 if no window is selected
{
    vector<Window>::reverse_iterator it;
        
    int t_Window = -1;
    
    int temp;
    
    for (it = m_Windows.rbegin(); it != m_Windows.rend(); ++it)
    {
        if (it->IsMinimized())
        {
            if (i_X >= it->GetX() && i_X <= it->GetX()+it->GetWidth() && i_Y >= it->GetY() && i_Y <= it->GetY()+21)
            {
                temp = CheckAndSelectItems(it, i_X, i_Y); //returns -2 on erase, 1 when being moved, 0 when buttons pressed
                if (temp != 1)
                    return temp;
                return (t_Window = it->GetID());
            }
        } else {
            if (i_X >= it->GetX() && i_X <= it->GetX()+it->GetWidth() && i_Y >= it->GetY() && i_Y <= it->GetY()+it->GetHeight())
            {
                temp = CheckAndSelectItems(it, i_X, i_Y); //returns -2 on erase, 1 when being moved, 0 when buttons pressed
                if (temp != 1)
                    return temp;
                return (t_Window = it->GetID());
            }
        }
    }
    
    return t_Window;
}

void WindowManager::MoveWindow(int i_X, int i_Y, int i_ID) //Finds window using ID then moves it
{
    if (i_ID == -1)
        return;
        
    vector<Window>::iterator it;
    
    for (it = m_Windows.begin(); it != m_Windows.end(); ++it)
    {
        if (it->GetID() == i_ID)
        {
            it->SetCords(i_X-(it->GetWidth()/2), i_Y-15);
            return;
        }
    }
}

void WindowManager::DrawWindows(ALLEGRO_FONT *font)
{
    vector<Window>::iterator it;
    for (it = m_Windows.begin(); it != m_Windows.end(); ++it)
    {   
        if (!it->IsMinimized())
        {
            al_draw_filled_rectangle(it->GetX(), it->GetY(), it->GetX()+it->GetWidth(), it->GetY()+it->GetHeight(), al_map_rgb(it->GetR(), it->GetG(), it->GetB()));  
            al_draw_rectangle(it->GetX(), it->GetY(), it->GetX()+it->GetWidth(), it->GetY()+it->GetHeight(), al_map_rgb(0, 0, 0), 3);
            al_draw_line(it->GetX(), it->GetY()+21, it->GetX()+it->GetWidth(), it->GetY()+21, al_map_rgb(0, 0, 0), 3);
            al_draw_rectangle(it->GetX()+5, it->GetY()+5, it->GetX()+17, it->GetY()+17, al_map_rgb(0, 0, 0), 2);
            al_draw_line(it->GetX()+5, it->GetY()+5, it->GetX()+17, it->GetY()+17, al_map_rgb(0, 0, 0), 2);
            al_draw_line(it->GetX()+17, it->GetY()+5, it->GetX()+5, it->GetY()+17, al_map_rgb(0, 0, 0), 2);
            al_draw_rectangle(it->GetX()+25, it->GetY()+5, it->GetX()+37, it->GetY()+17, al_map_rgb(0, 0, 0), 2);
            al_draw_line(it->GetX()+27, it->GetY()+11, it->GetX()+35, it->GetY()+11, al_map_rgb(0, 0, 0), 2);
            DrawButtons(it, font);
        } else {
            al_draw_filled_rectangle(it->GetX(), it->GetY(), it->GetX()+it->GetWidth(), it->GetY()+21, al_map_rgb(it->GetR(), it->GetG(), it->GetB()));
            al_draw_rectangle(it->GetX(), it->GetY(), it->GetX()+it->GetWidth(), it->GetY()+21, al_map_rgb(0, 0, 0), 3);
            al_draw_rectangle(it->GetX()+5, it->GetY()+5, it->GetX()+17, it->GetY()+17, al_map_rgb(0, 0, 0), 2);
            al_draw_line(it->GetX()+5, it->GetY()+5, it->GetX()+17, it->GetY()+17, al_map_rgb(0, 0, 0), 2);
            al_draw_line(it->GetX()+17, it->GetY()+5, it->GetX()+5, it->GetY()+17, al_map_rgb(0, 0, 0), 2);
            al_draw_rectangle(it->GetX()+25, it->GetY()+5, it->GetX()+37, it->GetY()+17, al_map_rgb(0, 0, 0), 2);
            al_draw_line(it->GetX()+27, it->GetY()+11, it->GetX()+35, it->GetY()+11, al_map_rgb(0, 0, 0), 2);
        }
            al_draw_text(font, al_map_rgb(0, 0, 0), it->GetX()+(it->GetWidth()/2), it->GetY()+5, ALLEGRO_ALIGN_CENTRE, it->GetTitle().c_str());
    }
}

int WindowManager::CheckAndSelectItems(vector<Window>::reverse_iterator it, int i_X, int i_Y) //Checks if a 'special' part of window is selected, IE exit - Buttons - Or the banner 
{
    vector<Button>::iterator itB;
    if (i_X >= it->GetX()+5 && i_X <= it->GetX()+17 && i_Y >= it->GetY()+5 && i_Y <= it->GetY()+17) //exit
    {
        if (!it->IsMaster())
        {
            m_Windows.erase((it+1).base());
            return -2;
        }
    }
    if (i_X >= it->GetX()+25 && i_X <= it->GetX()+37 && i_Y >= it->GetY()+5 && i_Y <= it->GetY()+17) //minimize
    {
        if (it->IsMinimized())
        {
            it->SetMinimized(false);
        } else {
            it->SetMinimized(true);
        }
        return -2;
    }
    if (i_X >= it->GetX() && i_X <= it->GetX()+it->GetWidth() && i_Y >= it->GetY() && i_Y <= it->GetY()+21) //being moved
    {
        return 1;
    }
    for (itB = it->m_Buttons.begin(); itB != it->m_Buttons.end(); ++itB) //button
    {
        if (i_X >= it->GetX()+itB->GetX() && i_X <= it->GetX()+itB->GetX()+itB->GetWidth() && i_Y >= it->GetY()+itB->GetY() && i_Y <= it->GetY()+itB->GetY()+itB->GetHeight())
        {
            if (itB->Exe != NULL)
                itB->Exe();
            return 0;
        }
    }
    return -1;
}

void WindowManager::AddButton(int i_X, int i_Y, int i_Width, int i_Height, string i_Text, Foo Exe, int i_ID)
{
    vector<Window>::iterator it;
    for (it = m_Windows.begin(); it != m_Windows.end(); ++it)
    {
        if (it->GetID() == i_ID)
        {
            it->AddButton(i_X, i_Y, i_Width, i_Height, i_Text, Exe);
            return;
        }
    }
}

void DrawButtons(vector<Window>::iterator it, ALLEGRO_FONT *font)
{
    vector<Button>::iterator itB;
    
    
    for (itB = it->m_Buttons.begin(); itB != it->m_Buttons.end(); ++itB)
    {
        al_draw_filled_rounded_rectangle(it->GetX()+itB->GetX(), it->GetY()+itB->GetY(), it->GetX()+itB->GetX()+itB->GetWidth(), it->GetY()+itB->GetY()+itB->GetHeight(), 5, 5, al_map_rgb(it->GetR()+15, it->GetG()+15, it->GetB()+15));
        al_draw_rounded_rectangle(it->GetX()+itB->GetX(), it->GetY()+itB->GetY(), it->GetX()+itB->GetX()+itB->GetWidth(), it->GetY()+itB->GetY()+itB->GetHeight(), 5, 5, al_map_rgb(0, 0, 0), 3);
        al_draw_text(font, al_map_rgb(0, 0, 0), it->GetX()+itB->GetX()+(itB->GetWidth()/2), it->GetY()+itB->GetY()+9, ALLEGRO_ALIGN_CENTRE, itB->GetText().c_str());
    }
    
}
I don't know much about windows, but maybe a vector of pointers to Window objects? That way you could pass the pointer, rather than all the variables that make up the object.

There are other data structures that are more efficient than vectors, but I can't imagine they would be any better unless you have lots and lots of windows.

This doesn't look like MS Windows code.

Whatever the system is - Aren't there standard ways of dealing with this?

HTH

Sorry i should have been more specific - It creates a window within the window
Heres a link of it: http://i.imgur.com/jdxFT.png

But can you give an example?

And im on Mac OS X
Sorry I don't know anything about MAC.

So the only advice I can give was this:

Aren't there standard ways of dealing with this?


Perhaps you could have a FindWindow function that would be called before the MoveWindow function - that way the move function won't have to iterate. Your DrawWindow function also iterates. It seems strange to me to have to draw each component like that.

Any way, I am going on about things that I don't specifically know about - My experience is more with Qt.

Maybe you can try a Mac OS X forum, if you don't get any help here?
Looks like more of an Allegro question then Mac.

Edit: To your actual question, vectors are pretty efficient as containers go. I don't know allegro but I'd guess if it is choppy while moving, maybe you are rendering the moving object to often?
Last edited on
@TheIdeasMan
Thanks you've given me a couple of ideas, but how do you suggest i draw them then ?
Again I am shooting in the dark here.

With Qt, you create a Dialog Box with it's controls, there are built in functions to display / move it / whatever. There is no need to draw individual lines etc.

Again I am not sure, but thought it was similar in MS Windows - at least with MS VC++ ?

As for the efficiency of containers, I was thinking more of the STL <map> or <set>, these use AVL trees and are much more efficient when storing a reasonable number of objects. Vectors can be terribly inefficient for finding & sorting, and especially if memory has to be reallocated for the whole vector.

But the choice of container, is not the OP's problem, IMO.
Topic archived. No new replies allowed.