Jump to content

  • Log In with Google      Sign In   
  • Create Account

Drawing tiles to a screen math problems


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 BaneTrapper   Members   -  Reputation: 1201

Like
0Likes
Like

Posted 24 August 2012 - 06:26 AM

Hello.
I have successfully stored and drew 4Regions of 25x25Tiles to screen and made camera to move around screen
Here is my function

But what i need is to draw only tiles that are on screen.
I am trying to save performance, cause i want to be efficient
Ani tips or hints would be userfull

Here is my function to draw all 4 region to screen;

void Area::DrawArea(int CamX, int CamY, App* obj_App)
{
    int TileID = 0; //Holds TileID;
    int RegionID = 0; //Holds RegionID
    int surfW = ((*GETP_surf_Tileset())->w / TILE_SIZE); //For SDL_Rect crop
    SDL_Rect crop,pos;
    crop.w = TILE_SIZE;
    crop.h = TILE_SIZE;
(*obj_App->GETP_surf_Screen());
    for(int a = 2; a < 6; a++) /*HomeMade math to draw to correct position will went : "x= 0 y = 0", "x = 1, y = 0", "x = 0 y = 1", "x = 1 y = 1"
    So we draw to correct place to screen*/
    {
	    for(int y = 0; y < REGION_SIZE/*25*/; y++)
	    {
		    for(int x = 0; x < REGION_SIZE/*25*/; x++)
		    {
			    if(RegionList[RegionID].TileList[TileID].Layer1 == -1) /* Check if tile loaded failed */
			    {
				    TileID++;
				    continue;
			    }
			    crop.x = (RegionList[RegionID].TileList[TileID].Layer1 % surfW) * TILE_SIZE;
			    crop.y = (RegionList[RegionID].TileList[TileID].Layer1 / surfW) * TILE_SIZE;
			    pos.x = (x * TILE_SIZE) - CamX + ((a % 2) * 1000);
			    pos.y = (y * TILE_SIZE) - CamY + (((a-2) / 2) * 1000);
			    SDL_BlitSurface(surf_Tileset, &crop, *obj_App->GETP_surf_Screen(), &pos);
			    TileID++;
		    }
	    }
	    TileID = 0; //We set TileID to 0 for next RegionLoop
	    RegionID ++; //We increment Region for next loop so we draw correct tiles
    }
}

Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


Sponsor:

#2 Bacterius   Crossbones+   -  Reputation: 8866

Like
0Likes
Like

Posted 24 August 2012 - 10:01 AM

Figure out which tile column is the first/last one on the screen and same for tile rows, then only loop across those. You can do this by comparing your tile's positions (top-left corner known from pos and width/height known from TILE_SIZE, which gives you the bottom-left corner) with the bounds of your window. Compare coordinates to do this, e.g. go over a tile row, and go "is the x coordinate less than zero, yes, keep going... yes, etc... ah, this one is more than zero, so this is the first tile in each row to be shown on the screen!" and so forth. You can actually also calculate quickly which tiles are the "boundary tiles" with a formula if your tiles are all the same size, but it's best you try and do it the explicit way first so you don't get confused.

Does it make sense?

Edited by Bacterius, 24 August 2012 - 10:03 AM.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#3 BaneTrapper   Members   -  Reputation: 1201

Like
0Likes
Like

Posted 24 August 2012 - 12:35 PM

Figure out which tile column is the first/last one on the screen and same for tile rows, then only loop across those. You can do this by comparing your tile's positions (top-left corner known from pos and width/height known from TILE_SIZE, which gives you the bottom-left corner) with the bounds of your window. Compare coordinates to do this, e.g. go over a tile row, and go "is the x coordinate less than zero, yes, keep going... yes, etc... ah, this one is more than zero, so this is the first tile in each row to be shown on the screen!" and so forth. You can actually also calculate quickly which tiles are the "boundary tiles" with a formula if your tiles are all the same size, but it's best you try and do it the explicit way first so you don't get confused.

Does it make sense?


Mhm interesting. But its not quite good approach to what i have currently done.
I am kinda rusty with english.

I am tinkering about 4h so far, Got done with so much math and stuff but its still missing some stuff i cant figure out.
As you are saying i do it like:
Store start location in loop
How much to loop in x , y
But i need to do it 4x times, DONE but i also need to shift tiles for Some amount so they are at correct position... and its complicated ^^.

Thanks on the tip, keep em coming.

Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS