My object array doesn't draw to the screen

Hey guys,

I've run into another error in my code. I'm basically defining classes 'Cube' and 'Enemy', passing them coordinates, then drawing them. Works fine with my Cube, as that's essentially my player's Cube. The problem is when I try to slip in an array for the Enemy class...

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
#include <SDL.h>

class Cube
{
public:
    Cube(int x,int y,int w,int h) { xPos = x; yPos = y; width = w; height = h; box.x = xPos; box.y = yPos; box.w = width; box.h = height;}
    SDL_Rect box;

private:
    int xPos;
    int yPos;
    int width;
    int height;
};

class Enemy
{
public:
    Enemy(int x,int y) { xPos = x; yPos = y; width = 25; height = 15; box.x = xPos; box.y = yPos; box.w = width; box.h = height;}
    SDL_Rect box;

    void SetX(int x) { xPos = x; }
    void SetY(int x) { yPos = x; }

private:
    int xPos;
    int yPos;
    int width;
    int height;
};

bool collision(SDL_Rect* rect1)
{
    if(rect1->x <= 0)
        return 1;
    if(rect1->x + rect1->w >= 640)
        return 1;
    if(rect1->y <= 0)
        return 1;
    if(rect1->y + rect1->h >= 480)
        return 1;
    return 0;
}

//bool collision(SDL_Rect* rect1, SDL_Rect* rect2)
//{
//    if(rect1->x <= rect2->x + rect2->w && rect1->x + rect1->w >= rect2->x && rect1->y <= rect2->y + rect2->h && rect1->y + rect1->h >= rect2->y)
//        return 1;
//    return 0;
//}

int main(int argc, char* argv[])
{
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Surface *screen;
    screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
    bool running = true;
    bool b[4] = {0,0,0,0};
    Uint32 start;
    int unsigned second = 1000;
    int unsigned fps = 30;
    SDL_Event event;
    Uint32 blue = SDL_MapRGB(screen->format,0,0,255);
    Uint32 black = SDL_MapRGB(screen->format,0,0,0);
    int unsigned enemyX = 150;
    int unsigned enemyY = 150;
    Cube * Box = new Cube(25,25,50,50);
    Enemy * Enemy[1];
    for(int unsigned i = 0; i < 1;i++)
        {
            Enemy[i] = new Enemy(enemyX, enemyY);
        }
    while(running)
    {
            start = SDL_GetTicks();
            while(SDL_PollEvent(&event))
            {
                switch(event.type)
                {
                case SDL_QUIT:
                    running = false;
                    break;
                case SDL_KEYDOWN:
                    switch(event.key.keysym.sym)
                    {
                    case SDLK_ESCAPE:
                        running = false;
                        break;
                    case SDLK_LEFT:
                        b[0] = 1;
                        break;
                    case SDLK_RIGHT:
                        b[1] = 1;
                        break;
                    case SDLK_UP:
                        b[2] = 1;
                        break;
                    case SDLK_DOWN:
                        b[3] = 1;
                        break;
                    }
                break;
                case SDL_KEYUP:
                    switch(event.key.keysym.sym)
                    {
                    case SDLK_LEFT:
                        b[0] = 0;
                        break;
                    case SDLK_RIGHT:
                        b[1] = 0;
                        break;
                    case SDLK_UP:
                        b[2] = 0;
                        break;
                    case SDLK_DOWN:
                        b[3] = 0;
                        break;
                    }
                    break;
                }
            }
            if(b[0] && collision(&Box->box) == 0){
                Box->box.x -= 5;
                if(collision(&Box->box) /*| collision(&Box->box,&Box1->box)*/)
                    Box->box.x += 5;
            }
            if(b[1] && collision(&Box->box) == 0){
                Box->box.x += 5;
                if(collision(&Box->box))
                    Box->box.x -= 5;
            }
            if(b[2] && collision(&Box->box) == 0){
                Box->box.y -= 5;
                if(collision(&Box->box))
                    Box->box.y += 5;
            }
            if(b[3] && collision(&Box->box) == 0){
                Box->box.y += 5;
                if(collision(&Box->box))
                    Box->box.y -= 5;
            }
            SDL_FillRect(screen,&screen->clip_rect,black);
            SDL_FillRect(screen,&Box->box,blue);
            for(int unsigned i = 0; i < 1; i++)
            {
                SDL_FillRect(screen,&Enemy[i]->box,blue);
            }
            SDL_Flip(screen);
            if(second/fps>(SDL_GetTicks()-start))
                SDL_Delay(second/fps-(SDL_GetTicks()-start));
    }
    SDL_QUIT;
    return 0;
}


Everything compiles fine... except there is no Enemy[i] drawn... Perhaps I'm just using incorrect syntax? Not sure guys... I would appreciate any input.

Secondly... the only warning that comes up is...

32: warning: no previous declaration for 'bool collision(SDL_Rect*)'

Can someone guide me to the issue and a solution, if willing? Much appreciated... thanks!

Edit: I reread what I posted and it seems that I was saying that the warning was somehow related to my problem. I do realize that it is not. It is a second issue I was hoping for some guidance on. Thanks!
Last edited on
What you declared on line 68 is an array consisting of 1 pointer. That pointer is pointing to garbage because you didn't allocate any memory for it to point to using new. Add Enemy[i] = new Enemy(enemyX, enemyY); in place of lines 71 and 72.
Ok thanks! I have done that, and now I am getting the error:

71|error: expected type-specifier before 'Enemy'|

I just don't know what this is getting at?
It could be because both the array and the class are named the same thing.
That was actually it! Amazing...

question...

Was that an oddball guess? Was it out of experience? What led you to say that? Just a proficiency curiosity...

Anyways, thanks!
It was partly a guess, since nothing else seemed to be wrong, then I went and tried compiling something similar (http://ideone.com/fsVoRK) and got the same result.

It's generally a bad idea to have two things named the same (barring overloaded functions etc.)
Last edited on
I see.. well it definitely makes sense.

So how about the for loop I've implemented? I used i < 1 at first, but now I want to plug in my array... I've changed the code to:

1
2
3
4
5
6
Enemy * opponent[8];
    for(int unsigned i = 0; i < opponent[i]//something here//();i++)
        {
           opponent[i] = new Enemy(enemyX,enemyY);
           enemyX = enemyX + 50;
        }


I've known people to use .size();, but that won't work with my referencing. What is convention for this function? I'm trying to run the loop through the array, and don't know how to retreive the size for the loop.

I get: 'class Enemy' has no member named 'size'
Last edited on
Actually, after some different research, I found the sizeof(); function.

Totally my mistake on that one. I wasn't asking the right questions.

Edit: Not correct. sizeof(); gives the number of bytes used, if I understand it correctly. So in my code, it shows 4 boxes despite having an array of 8 opponents. It still eludes me...
Last edited on
So I have changed my code again. This time I went with another approach:

1
2
3
4
5
6
7
int unsigned opponents = 10;
    Enemy * opponent[opponents];
    for(int unsigned i = 0; i < opponents;i++)
        {
           opponent[i] = new Enemy(enemyX,enemyY);
           enemyX = enemyX + 50;
        }


This way, I can control the number of opponents in the same manner as I would have before. But just for education's sake, maybe someone could guide me as to the correct solution for my previous issue? Thanks guys, I really appreciate it.
Your last post is a good way to solve your problem, although be sure to add const to line 1. Static arrays need to be initialized with a constant size. You also don't want to accidentally change opponents during runtime and have the for loops that use it to go out of bounds.

sizeof does indeed return the size of an object in bytes. It's possible to figure out the size of an array by dividing the total size of the array by the size of one of the objects:
1
2
int numbers[10];
int size = sizeof(numbers) / sizeof(numbers[0]); // size will be 10 


But keep in mind that this will only work with arrays, not pointers. This is because sizeof returns the size of the pointer itself in bytes.
Last edited on
Topic archived. No new replies allowed.