[Solved] [C] Little problem with Struct and memory

[Solved] [C] Little problem with Struct and memory

Postby Pix3l » 10 Aug 2011, 18:39

Hello to everybody, i was trying to make a space invaders like game, starting with simple shapes.
I allocated statically 54 structs of Enemy type for handle every single enemy on the screen, but it seems that they go in conflict with Bullet struct in memory or something else.

As you can see in the first image on the left, the green top square is an enemy, the middle one is a bullet shooted from the player (red square).
In the image on the right, you can see the execution of the program without the rendering for the enemy, without it, the bullet works fine.

ImageImage

I can't really figure it out, i can making all works fine by allocating only one struct of Enemy tipo, but it's not what i need...
Can someone help me to understand?
I have compiled the code below with gcc (Debian 4.3.2-1.1) 4.3.2 and SDL 1.2.14 version.

{l Code}: {l Select All Code}
//gcc -o invaders invaders.c -lSDL

#include <stdio.h>
#include <SDL/SDL.h>

#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 240

#define TILE_SIZE 10
#define BULLET_W 2
#define BULLET_H 6

#define MAX_ALIENS 54 // Numero massimo di invasori alieni

//Global variables
int repeat, quit=0;
SDL_Event event;
SDL_Surface *screen;
SDL_Rect rect;
Uint8 *keystate; // keyboard state
int bKeyATick;
int t, tl = 0, frequency = 1000 / 100, temp, t2;

struct _Player
{
   int x, y, incX, incY;
} Player;

struct _Bullet
{
   int x, y, alive;
} Bullet;

struct _Enemy
{
   int x, y, alive, type;
} Enemy[MAX_ALIENS];

//Object-to-object bounding-box collision detector
//Si necessita' di aggiungere la coppia X/Y + W/H per un calcolo corretto!!!!
int rectCollision(int x1, int y1, int w1, int h1,
                  int x2, int y2, int w2, int h2)
 {
    if (y1+h1 < y2) return 0;
    if (y1 > y2+h2) return 0;
    if (x1+w1 < x2) return 0;
    if (x1 > x2+w2) return 0;
   
    return 1;
 }

void DrawRect(int x, int y, int width, int height, int color)
{
    rect.x = x;
    rect.y = y;
    rect.w = width;
    rect.h = height;
    SDL_FillRect(screen, &rect, color);
}

void drawInvaders()
{
      int i;
      
   for(i=0; i<MAX_ALIENS+1; i++)
      DrawRect(Enemy[i].x, Enemy[i].y, TILE_SIZE, TILE_SIZE, 0x00FF64);

}

void draw()
{
   DrawRect(Player.x, Player.y, TILE_SIZE, TILE_SIZE, 0xff0000);
     
    if(Bullet.alive)
     DrawRect(Bullet.x, Bullet.y, BULLET_W, BULLET_H, 0xFFFF66);
   
   drawInvaders(); //Comment here for make it works

}

void shoot()
{
   // Check if exist any empty slot for Initialize a bullet
    if(!Bullet.alive)
    {
       Bullet.alive = 1;
       //Bullet start position
       Bullet.x = Player.x + (TILE_SIZE/2 - BULLET_W/2);
      Bullet.y = Player.y - TILE_SIZE ;
    }
   
}

void moveBullet()
{
   //Move bullet if there's one at least alive
   int i;
    if(Bullet.alive==1)
    {
      Bullet.y -= 4;
      // Bullet Destroy condition
      if(Bullet.y <= 0) Bullet.alive = 0;
    }
}

void move()
 {
  if(Player.x >= 0 || Player.x + TILE_SIZE <= SCREEN_WIDTH)
   {
        Player.x+=Player.incX;
   }
 }

int getInput()
{
   while(SDL_PollEvent(&event))
      {
         if (event.type == SDL_QUIT ||
         (event.type == SDL_KEYDOWN &&
         event.key.keysym.sym == SDLK_ESCAPE)) quit = 1;      /* Window closed */
      }
            // Grab a keystate snapshot
                keystate = SDL_GetKeyState( NULL );
            /* Handle key presses */
                 if (keystate[SDLK_LEFT] && (Player.x != 0))
                   Player.incX=-1;
                 if (keystate[SDLK_RIGHT] && (Player.x + TILE_SIZE != SCREEN_WIDTH))
                   Player.incX=+1;
                 if(!keystate[SDLK_LEFT] && !keystate[SDLK_RIGHT])
                  {
                     Player.incX=0;
              }
                 if(!keystate[SDLK_UP] && !keystate[SDLK_DOWN])
                  {
                     Player.incY=0;
              }
             if(keystate[SDLK_f])
                  {
                     //Shoot
                     if(t2+100<SDL_GetTicks())
                     {
                      shoot();
                      t2=SDL_GetTicks ();
                }
                     
              }
             move();
}

int fps_sync (void)
{

   t = SDL_GetTicks ();

   if (t - tl >= frequency)
   {
      temp = (t - tl) / frequency;
      tl += temp * frequency;
      return temp;
   }
   else
   {
      SDL_Delay (frequency - (t - tl));
      tl += frequency;
      return 1;
   }
}

int main()
{
  // init video stuff
   if(SDL_Init(SDL_INIT_VIDEO) != 0)
   {
      fprintf(stderr, "Can't initialize SDL: %s\n", SDL_GetError());
      exit(-1);
   }
   atexit(SDL_Quit);

   // init screen
   screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, SDL_SWSURFACE);
   if(screen == NULL)
   {
      fprintf(stderr, "Can't initialize SDL: %s\n", SDL_GetError());
      exit(-1);
   }

   SDL_WM_SetCaption("RetroTest", "RetroTest");
   
   //Init stuffs
   Player.x = 20;
   Player.y = SCREEN_HEIGHT - TILE_SIZE*2;
   
    // Initialize the bullet
   Bullet.alive = 0;
   
   int i;
   for(i=0; i<MAX_ALIENS+1; i++)
    Enemy[i].x = Enemy[i].y = Enemy[i].alive = 0;
   
  //main game loop
  while(!quit)
   {
      repeat = fps_sync ();
      int i;
      for (i = 0; i < repeat; i ++)
       {
           getInput();
           moveBullet(); //Too high cpu usage
       }
      
        // Cancella tutto lo schermo di gioco
      SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
      //draw to the screen
      draw();
      // Aggiorna lo schermo di gioco
      SDL_Flip(screen);
      //printf("Bullet %d %d\n", Bullet.x, Bullet.y);
   }
      SDL_Quit();
   return 0;
}


Thanks for help.
Last edited by Pix3l on 10 Aug 2011, 21:16, edited 1 time in total.
http://www.pix3lworkshop.altervista.org/ - Your 8bit choice since 2006!
User avatar
Pix3l
 
Posts: 55
Joined: 10 Sep 2010, 21:00
Location: Italy

Re: [C] Little problem with Struct and memory

Postby amuzen » 10 Aug 2011, 19:04

Change this line
{l Code}: {l Select All Code}
for(i=0; i<MAX_ALIENS+1; i++)

to this
{l Code}: {l Select All Code}
for(i=0; i<MAX_ALIENS; i++)


Imagine that MAX_ALIENS=0. The loop should terminate immediately but with that +1 makes it execute once regardless.
User avatar
amuzen
LoS Moderator
 
Posts: 327
Joined: 05 Dec 2009, 02:49

Re: [C] Little problem with Struct and memory

Postby Pix3l » 10 Aug 2011, 21:14

amuzen {l Wrote}:Change this line
{l Code}: {l Select All Code}
for(i=0; i<MAX_ALIENS+1; i++)

to this
{l Code}: {l Select All Code}
for(i=0; i<MAX_ALIENS; i++)


Imagine that MAX_ALIENS=0. The loop should terminate immediately but with that +1 makes it execute once regardless.


Oh yes, i have forget to correct that, what a stupid that am...

Thanks for your help pal :]
http://www.pix3lworkshop.altervista.org/ - Your 8bit choice since 2006!
User avatar
Pix3l
 
Posts: 55
Joined: 10 Sep 2010, 21:00
Location: Italy

Who is online

Users browsing this forum: No registered users and 1 guest