Sign in to follow this  
r0bbb

Unity slow isometric sdl drawing

Recommended Posts

r0bbb    122
hi i put this code together quickly, but it seams to run reallly slow. im not really doing anything that processor intensive. when i get to an edge of the map it runs fast, obviously suggesting that something in the drawing code is slowing it down, but as im new to this, i cant see what. also when i add the fullscreen flag, nothing is drawn. thanks in advance heres the code:
#include <stdio.h>
#include <stdlib.h>

#include "SDL.h"

SDL_Surface *grass;
SDL_Surface *stone;

SDL_Surface *screen;

int tileW = 36;
int tileH = 18;
int mapX = 60;
int mapY = 10;
int movement= 2;

//ignore the way this is done, just set up to test a couple of tiles.
int mapArray[100][100];

void Slock(SDL_Surface *screen)
{
  if ( SDL_MUSTLOCK(screen) )
  {
    if ( SDL_LockSurface(screen) < 0 )
    {
      return;
    }
  }
}

void Sulock(SDL_Surface *screen)
{
  if ( SDL_MUSTLOCK(screen) )
  {
    SDL_UnlockSurface(screen);
  }
}

SDL_Surface * ImageLoad(char *file)
{
  SDL_Surface *temp1, *temp2;
  temp1 = SDL_LoadBMP(file);
  temp2 = SDL_DisplayFormat(temp1);
  SDL_FreeSurface(temp1);
  return temp2;
}

int InitImages()
{
  grass = ImageLoad("grass.bmp");
  stone = ImageLoad("stone.bmp");
  return 0;
}



void DrawIMG(SDL_Surface *img, int x, int y)
{
  SDL_Rect dest;
  dest.x = x;
  dest.y = y;
  SDL_BlitSurface(img, NULL, screen, &dest);
  SDL_UpdateRects(screen, 1, &dest);
}

void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2)
{
  SDL_Rect dest;
  dest.x = x;
  dest.y = y;
  SDL_Rect dest2;
  dest2.x = x2;
  dest2.y = y2;
  dest2.w = w;
  dest2.h = h;
  SDL_BlitSurface(img, &dest2, screen, &dest);
  SDL_Flip(screen);
}

void DrawMap(){

     Slock(screen);
     
     SDL_SetColorKey(stone, SDL_SRCCOLORKEY, SDL_MapRGB(stone->format, 255, 0, 255));
     SDL_SetColorKey(grass, SDL_SRCCOLORKEY, SDL_MapRGB(grass->format, 255, 0, 255));

     for(int j = 0; j < 100; j++){
          for(int i = 0; i < 100; i++){
               if(mapArray[i][j] == 0){
                    if(((tileW/2)*(j-i))+mapX < (640+tileW) && ((tileW/2)*(j-i))+mapX > (0-tileW)){
                         if(((tileH/2)*(j+i))+mapY < (480+tileH) && ((tileH/2)*(j+i))+mapY > (0-tileH)){
                              DrawIMG(stone, ((tileW/2)*(j-i))+mapX, ((tileH/2)*(j+i))+mapY);
                         }
                    }

               }
               else{
                    DrawIMG(grass, ((tileW/2)*(j-i))+mapX, ((tileH/2)*(j+i))+mapY);
               }
          }
     }

     Sulock(screen);
}

int main(int argc, char *argv[]){


     Uint8* keys;

//ignore the way this is done, just set up to test a couple of tiles.
     mapArray[1][1] = 1;
     mapArray[2][1] = 1;
     mapArray[4][1] = 1;
     mapArray[50][50] = 1;
     mapArray[40][35] = 1;
     mapArray[50][40] = 1;
     mapArray[40][35] = 1;

  if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
  {
    printf("Unable to init SDL: %s\n", SDL_GetError());
    exit(1);
  }
  atexit(SDL_Quit);

  screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE|SDL_HWPALETTE|SDL_DOUBLEBUF|SDL_ANYFORMAT);

  if ( screen == NULL )
  {
    printf("Unable to set 640x480 video: %s\n", SDL_GetError());
    exit(1);
  }

  InitImages();
  DrawMap();

  int done=0;

     while(done == 0){
          SDL_Event event;

          while ( SDL_PollEvent(&event) ){

               if ( event.type == SDL_QUIT )  {  done = 1;  }

               if ( event.type == SDL_KEYDOWN ){
                    if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
               }
          }
    keys = SDL_GetKeyState(NULL);

    if ( keys[SDLK_UP] ) { 
         mapY = mapY-movement; 
    }
    if ( keys[SDLK_DOWN] ) { 
         mapY = mapY+movement; 
    }

    if ( keys[SDLK_LEFT] ) { 
         mapX = mapX-movement; 
    }

    if ( keys[SDLK_RIGHT] ) { mapX = mapX+movement; 
    }

    DrawMap();


 }

  return 0;
}


edited by evolutional: Source tags! See the FAQ for more information [Edited by - evolutional on November 25, 2004 5:36:36 PM]

Share this post


Link to post
Share on other sites
tentoid    364
Quote:
Original post by r0bbb

screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE|SDL_HWPALETTE|SDL_DOUBLEBUF|SDL_ANYFORMAT);

this will not probably help much but use SDL_HWSURFACE instead.. And use [ source lang="cpp"] or [ code] tags

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Hi! Your code is probabally slow because of your draw function:

void DrawMap()
{
// Slock(screen); format, 255, 0, 255));
SDL_SetColorKey(grass, SDL_SRCCOLORKEY, SDL_MapRGB(grass->format, 255, 0, 255)); (0-tileW))
{
if(((tileH/2)*(j+i))+mapY (0-tileH))
{
DrawIMG(stone, ((tileW/2)*(j-i))+mapX, ((tileH/2)*(j+i))+mapY);
}
}
}
else
{
DrawIMG(grass, ((tileW/2)*(j-i))+mapX, ((tileH/2)*(j+i))+mapY);
}
}
}
// Sulock(screen); <---- NOT NEEDED
}

Once you make those changes, you should see IMMEDIATE improvments. furthermore, you should take a look at enabling opengl blitting in your game/app so it is faster and looks better. If you have any questions or comments, email me, dr3w_b3nton AT hotmail.com

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
ok sorry, none of my comments made it to the final post :/ and I cant get my login to work so ill try this again, if you need better clarifcations email me por favor:

Hi! Your code is probabally slow because of your draw function:

void DrawMap()
{
Slock(screen); **NOT NEEDED**
** SDL_SetColorKey(stone, SDL_SRCCOLORKEY, SDL_MapRGB(stone->format, 255, 0, 255));
SDL_SetColorKey(grass, SDL_SRCCOLORKEY, SDL_MapRGB(grass->format, 255, 0, 255)); ** ONLY CALL THESE FUNCTIONS ONCE AFTER YOU LOAD THE IMAGES **

**YOU DONT NEED TO DRAW 100,000 tiles EACH time, limit yourself to the screen resolution / tile dimenstions, ex: 640x480 screen and 32x32 tiles, you will only need to draw '640/32' x '480/32' tiles.
for(int j = 0; j (0-tileW))
{
if(((tileH/2)*(j+i))+mapY (0-tileH))
{
DrawIMG(stone, ((tileW/2)*(j-i))+mapX, ((tileH/2)*(j+i))+mapY);
}
}
}
else
{
DrawIMG(grass, ((tileW/2)*(j-i))+mapX, ((tileH/2)*(j+i))+mapY);
}
}
}
// Sulock(screen); ---- NOT NEEDED ----
}

Once you make those changes, you should see IMMEDIATE improvments. furthermore, you should take a look at enabling opengl blitting in your game/app so it is faster and looks better. If you have any questions or comments, email me, dr3w_b3nton AT hotmail.com

Share this post


Link to post
Share on other sites
Frank Henry    186
You also have a LOT of redundant code.

tileW/2 & tileH/2 is used multiple times.
Just create an int for each and save the results there.

Same with j+i and j-i, 640+tileW and 480+tileH, 0-tileW and 0-tileH (these 4 really only need calculating once). Sure not that expensive but if you do it 100x100 times it sums up.

the data in this code
DrawIMG(stone, ((tileW/2)*(j-i))+mapX, ((tileH/2)*(j+i))+mapY);
has already been calculated in the ifs.. if you use the variables you would not need to calculate this stuff.
Same for the grass drawing.

General tip:
Make your code readable, then make it fast.
Go through what you are doing step by step and try to avoid such long expressions.

Had you gone through it step by step and created variables for each you could have avoided above problems.

Here is how I might do it.
WARNING: dumb variable names ;)

// only need these once
int half_tileW = tileW/2;
int half_tileH = tileH/2;

int screen_width_P_tileW = 640+tileW;
int screen_height_P_tileH = 480+tileH;

// variables outside the loop so they only get initialized once

int half_tileW_jPi = 0; // replaces half_tileW*(j+i)
int half_tileW_jMi = 0; // replaces half_tileW*(j-i)

int half_tileH_jPi = 0; // replaces half_tileH*(j+i)
int half_tileH_jMi = 0; // replaces half_tileH*(j-i)

int half_tileW_jMiPmapX = 0; // replaces half_tileW_jMi + mapX
int half_tileH_jMiPmapY = 0; // replaces half_tileH_jPi + mapY


for(int j = 0; j < 100; j++){
for(int i = 0; i < 100; i++)
{
// put thise here because you will need them everytime
half_tileW_jMi = half_tileW*(j-i);
half_tileH_jPi = half_tileH*(j+i);

half_tileW_jMiPmapX = half_tileW_jMi + mapX;
half_tileH_jPiPmapY = half_tileH_jPi + mapY;

if(mapArray[i][j] == 0)
{
half_tileW_jPi = half_tileW*(j+i);

half_tileH_jMi = half_tileH*(j-i);

// this is how I format my code
if( ( half_tileW_jMiPmapX ) < screen_width_P_tileW &&
( half_tileW_jMiPmapX ) > -tileW )
{
if( ( half_tileH_jPiPmapY ) < screen_height_P_tileH &&
( half_tileH_jPiPmapY ) > -tileH )
{
// calculations only done once, so we keep
DrawIMG(stone, ( half_tileW_jPi + mapX, half_tileH_jPi + mapY);
}
}
}
else
{
DrawIMG(grass, (half_tileW_jMiPmapX, half_tileH_jPiPmapY);
}
}
}

Share this post


Link to post
Share on other sites
TravisWells    276
Remove the SDL_UpdateRect and SDL_Flip from both the versions of DrawIMG. You're syncing to a screen refresh on EVERY TILE, even if that tile is off-screen.

Instead, do the SDL_Flip() at the end of DrawMap.

Also, do not lock the screen. (Slock/Sulock)
You only need to lock a surface if you require pixel-level access to it.
SDL_BlitSurface locks and unlocks the surface if necessary, and if it isn't necessary... it might slow it down

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By Affgoo
      About us:
      We are a team of 14 developers developing multiple mid scope games both are over halfway complete. We polish all of our games and focus on quality.


      We are a small team, everyone currently on the team and future teammates must be interested in game development as a whole and not just one role, being a small indie company it is very important that you can wear a few hats and not just one. Everyone on our team is a game dev.

      looking for:
      3d Artist (hand painting a huge plus)
      3d Animator
      entry level Software engineer with reasonable skills in c# / shaders. 

      ^ requirement for all positions: A true love of game development and to be very self motivated.
      We are a very active team, you must be too. 


      If interested or for more information add me on skype: nicholas.boucher4


      Atlas Sentry art style: (art complete)(code complete)
      http://www.slidedb.com/games/atlas-sentry

      Rat n Gat art style: In Devlopment

    • By EvaBalikova
      Main menu in Feudal Alloy. 
      twitter
    • By Jcyshadow97
      Hi guys,i m looking for someone that can work with me on a "top-down" multiplayer fps as 2d and 3d artist.I used photon server and i can take the part of programming.For now i made only the basic gameplay of the game that include shooting,switch weapon and and damage player.If someone can help me please contact me via e mail: 270514974@libero.it.
      I really appreciate your collaboration and hope you have a good day.....
      Thanks for you time to read the post
      At the bottom i attach some screenshot of the current game,i m sorry that i can't attach a video...



    • By Raptor42
      I'm looking to form a new game development team, mostly for training purposes.
      About me:
      I'm a student - Unity C# developer, who worked part-time in this industry for a couple of years already. I've been a lead developer in many "random collab groups" as well as a few companies. I specialize in creating 2D games for Android, but I'm looking forward to trying out new things - especially 3D development.
      Currently, I've got one Android game close to a release so I'd work for this team in my spare time. 
      About the project:
      I've been thinking about creating a simple tycoon-like simulation game for Android (and PC eventually), inspired by the Game Dev Story (initially released by Kairosoft in 1997) https://en.wikipedia.org/wiki/Game_Dev_Story 
      I haven't done much planning though, therefore I'm looking forward to hearing out your ideas.
      Right now, I've only created a test 3D scene using placeholder models and implemented a simple pathfinding system for me to play around with:
      https://i.imgur.com/xAd0l4o.png
      https://i.imgur.com/nHZerOT.png
      I'm looking to work with people who are:
      - willing to take a position of a: 3D modeller/2D artist/Designer
      - not necessarily very experienced, but eager to learn and improve their skills
      - active - check in at least once a day
      If you'd like to apply for a different position which I didn't list here, you are welcome to contact me as well.
      While this project is created mostly for learning purposes, if we ever get to release it and generate any revenue - you will recieve a certain percentage of it.
       
      To Apply:
      Send an email to rk.softwaredev@gmail.com
      Introduce yourself and attach an example of your work (if you have any)
    • By EvaBalikova
      Hello.

      We're a two-member team and I'd like to introduce you our game - Feudal Alloy. 
      It's a metroidvania-style action RPG with fishbowl-powered medieval robots.

      We've been working on the project for a few months. I'd be happy for any feedback.
      teaser
      homepage
      twitter
      facebook
       
      Our Steam page is public now: 
      steam link
       
       



  • Popular Now