Suggestions/Optimizations?

Heyy rrybody,
Another post about my window manager if you saw my last post

I was wondering if any of you had any suggestions or optimization you could tell me about.
I dont really like how i loop thru every vector almost every function...
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
//
//  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>::iterator it;
    
    int t_Window = -1;
    
    int temp;
    
    for (it = m_Windows.begin(); it != m_Windows.end(); ++it)
    {
        if (i_X >= it->GetX() && i_X <= it->GetX() + it->GetWidth())
        {
            if (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;
                if (temp == 1)
                    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>::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);
            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());
    }
    
}

Heres a pic of it if anybody cares :p
http://imgur.com/i66xq
Last edited on
Any constructive criticism would be nice :p
Topic archived. No new replies allowed.