collision detector

hi

i was trying to make a basic collision detector and i guess i made a pointer usage mistake which is i m very bad at.

here is the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bool fge_CollisionDetector(SDL_Texture * obj1, SDL_Texture * obj2)
{
    int right1, right2,
        left1, left2,
        top1, top2,
        bottom1, bottom2;

    right1  = obj1->x + obj1->w;              right2  = obj2->x + obj2->w;
    left1   = obj1->x;                        left2   = obj2->x;
    top1    = obj1->y;                        top2    = obj2->y;
    bottom1 = obj1->y + obj1->h;              bottom2 = obj2->y + obj2->h;

    if(left1 <= right2)     return true;
    else
        if(left2 <= right1) return true;
    else
        if(top1 <= bottom2) return true;
    else
        if(bottom1 <= top2) return true;
    else
                            return false;
}


and the error it gives
C:\Users\Ceset\Desktop\Pong\include\..\FGE\fge_collision.h||In function 'bool fge_CollisionDetector(SDL_Texture*, SDL_Texture*)':|
C:\Users\Ceset\Desktop\Pong\include\..\FGE\fge_collision.h|17|error: invalid use of incomplete type 'SDL_Texture {aka struct SDL_Texture}'|
C:\SDL 2.0.0\include\SDL2\SDL_render.h|127|error: forward declaration of 'SDL_Texture {aka struct SDL_Texture}'|
C:\Users\Ceset\Desktop\Pong\include\..\FGE\fge_collision.h|17|error: invalid use of incomplete type 'SDL_Texture {aka struct SDL_Texture}'|
C:\SDL 2.0.0\include\SDL2\SDL_render.h|127|error: forward declaration of 'SDL_Texture {aka struct SDL_Texture}'|
.//same error all over
.//same error all over
.//same error all over
.//same error all over
||=== Build finished: 24 errors, 11 warnings (0 minutes, 1 seconds) ===|
Last edited on
error's are in header file. post the complete file
You need to show more than this. For one thing your error messages seem to point exclusively at header files, which you haven't shown.
It appears you've not actually defined SDL_Texture anywhere, but we can't know for sure what's wrong without more information.
well first of all this is the header file and if you want the full code

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
#ifndef FGE_COLLISION_H
#define FGE_COLLISION_H

#ifndef __cplusplus
#error A cplusplus compiler is required
#endif

#include "fge_includes.h"

bool fge_CollisionDetector(SDL_Texture * obj1, SDL_Texture * obj2)
{
    int right1, right2,
        left1, left2,
        top1, top2,
        bottom1, bottom2;

    right1  = obj1->x + obj1->w;              right2  = obj2->x + obj2->w;
    left1   = obj1->x;                        left2   = obj2->x;
    top1    = obj1->y;                        top2    = obj2->y;
    bottom1 = obj1->y + obj1->h;              bottom2 = obj2->y + obj2->h;

    if(left1 <= right2)     return true;
    else
        if(left2 <= right1) return true;
    else
        if(top1 <= bottom2) return true;
    else
        if(bottom1 <= top2) return true;
    else
                            return false;
}


#endif // FGE_COLLISION_H 


and fge_includes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef FGE_INCLUDES_H
#define FGE_INCLUDES_H

#ifndef __cplusplus
#error A cplusplus compiler is required
#endif

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <string>
#include <iostream>
#include <fstream>

#endif // FGE_INCLUDES_H 


and the error is given even before i use this function. right after i add the header
Last edited on
You don't include the SDL header that defines the SDL_Texture. Identify the header and include it.
@keskiverto if i did not misunderstand you meant add #include <SDL2/SDL_render.h> to the fge_includes.h file right?

i did as you told(which did not make sense to me because SDL.h already includes these headers) and it gives the same error. if you meant sth else can you give a little further info?
What we need to see is the header that declares or defines SDL_Texture. It appears that you've forward declared it somewhere, so you could use a pointer to it, but the real declaration has not yet been seen by the compiler.

Can you include the file with the declaration of struct SDL_Texture or class SDL_Texture, and show us said file if you can.

hmm. this is a sdl library header that declares it. so when i added SDL.h it should also be added. and of course SDL_Texture should be declared.

and it is sure that SDL is not the one fault here so maybe i m making a usage mistake about SDL_Texture or an algorithm mistake. wiki doesnt have much info but let me make some tests. i will come up with more info and maybe with the solution
Last edited on
┬┐who told you that a texture has an `x' field?
Keep in mind that it is an OO approach codified in C. So you call methods passing around the `this' pointer.

You should never dereference that pointer,
@ne555 thx.

after a nap and a bath. after refreshment i asked myself what the hell was i thinking.

thx again
Last edited on
Topic archived. No new replies allowed.