Hi,
SDL provides me this struct:
typedef struct SDL_Rect {
Sint16 x, y;
Uint16 w, h;
} SDL_Rect;
I want to create a new SDL_Rect
on the heap as class variable:
// Forward declaration
private:
SDL_Rect *m_pcScreenRect;
And in the constructor I do this:
/* Screen Rectangle (for clearing) */
m_pcScreenRect = new SDL_Rect;
m_pcScreenRect->x = 0;
m_pcScreenRect->y = 0;
m_pcScreenRect->w = 800;
m_pcScreenRect->h = 600;
printf("Rect(x:%d, y:%d, w:%d, h:%d)\n", m_pcScreenRect->x, m_pcScreenRect->y, m_pcScreenRect->w, m_pcScreenRect->h);
Which prints Rect(x:0, y:0, w:800, h:600)
So this is correct.
Problem 1
But when I don't initialize x
and y
, it prints rubbish numbers like:
Rect(x:-11280, y:63, w:800, h:600)
Rect(x:25584, y:167, w:800, h:600)
Rect(x:-11280, y:40, w:800, h:600)
// This is just, run, terminate, run, terminate, ....
And I thought the default value for an int is 0
?
Problem 2
In my gameloop, I have the same line to check the values of the SDL_Rect. In the loop I get results like this:
Clear (x:0, y:40, w:0, h:560)
Clear (x:0, y:99, w:0, h:501)
Clear (x:0, y:55, w:0, h:545)
Clear (x:0, y:55, w:0, h:545)
// Again: run, terminate, run, terminate....
When my constructor looks like this:
/* Screen Rectangle (for clearing) */
m_pcScreenRect = new SDL_Rect;
//m_pcScreenRect->x = 0;
//m_pcScreenRect->y = 0;
m_pcScreenRect->w = 800;
m_pcScreenRect->h = 600;
And I get normal results when I uncomment the two lines:
/* Screen Rectangle (for clearing) */
m_pcScreenRect = new SDL_Rect;
m_pcScreenRect->x = 0;
m_pcScreenRect->y = 0;
m_pcScreenRect->w = 800;
m_pcScreenRect->h = 600;
Does this problem something have to do with new
or with the datatypes (Uint16, and a normal int). If it is with the datatypes, how to solve it?
Thanks would be very appreciated! (Haha!)
Thanks,
Any help would be very appreciated!
Extra Question:
I have to define all my variables in C++.
But from where do the random numbers come?
I'm using g++
and gcc
in Linux.