diff options
| author | zlago | 2025-01-28 18:35:26 +0100 | 
|---|---|---|
| committer | zlago | 2025-01-28 18:35:26 +0100 | 
| commit | f2cae9dbd96fde6eeb47d2f3ba3404944230dfba (patch) | |
| tree | 8d7489133d6c5e0937a4e4408640ff6afd2a3683 | |
| parent | f67cdf08cb3dba9c176eb81e137e2da4648d9ace (diff) | |
fix rendering tiny rooms
| -rw-r--r-- | src/main.c | 49 | ||||
| -rw-r--r-- | src/res/test.tmx | 4 | ||||
| -rw-r--r-- | src/res/tiny.tmx | 54 | ||||
| -rw-r--r-- | src/tilemap.c | 26 | 
4 files changed, 86 insertions, 47 deletions
| @@ -240,10 +240,19 @@ int game_load_level(char *level) {  			game_state = STATE_FADE_OUT;  			fade = 255; -			int x = (entities.player[0].x / 16) - (WINDOW_WIDTH / 2); -			int y = (entities.player[0].y / 16) - (WINDOW_HEIGHT / 2); -			if (x < 0) {x = 0;} else if (x + WINDOW_WIDTH > tilemap->width * 8) {x = tilemap->width * 8 - WINDOW_WIDTH;} -			if (y < 0) {y = 0;} else if (y + WINDOW_HEIGHT > tilemap->height * 8) {y = tilemap->height * 8 - WINDOW_HEIGHT;} +			int x, y; +			if (tilemap->width < WINDOW_WIDTH / 8) { +				x = -((WINDOW_WIDTH - tilemap->width * 8) / 2); +			} else { +				x = (entities.player[0].x / 16) - (WINDOW_WIDTH / 2); +				if (x < 0) {x = 0;} else if (x + WINDOW_WIDTH > tilemap->width * 8) {x = tilemap->width * 8 - WINDOW_WIDTH;} +			} +			if (tilemap->height <= WINDOW_HEIGHT / 8) { +				y = -((WINDOW_HEIGHT - tilemap->height * 8) / 2); +			} else { +				y = (entities.player[0].y / 16) - (WINDOW_HEIGHT / 2); +				if (y < 0) {y = 0;} else if (y + WINDOW_HEIGHT > tilemap->height * 8) {y = tilemap->height * 8 - WINDOW_HEIGHT;} +			}  			game_render(framebuffer, x, y);  			SDL_SetRenderTarget(renderer, NULL); @@ -654,10 +663,18 @@ void main_loop(void) {  					}  				} -				x = (entities.player[0].x / 16) - (WINDOW_WIDTH / 2); -				y = (entities.player[0].y / 16) - (WINDOW_HEIGHT / 2); -				if (x < 0) {x = 0;} else if (x + WINDOW_WIDTH > tilemap->width * 8) {x = tilemap->width * 8 - WINDOW_WIDTH;} -				if (y < 0) {y = 0;} else if (y + WINDOW_HEIGHT > tilemap->height * 8) {y = tilemap->height * 8 - WINDOW_HEIGHT;} +				if (tilemap->width < WINDOW_WIDTH / 8) { +					x = -((WINDOW_WIDTH - tilemap->width * 8) / 2); +				} else { +					x = (entities.player[0].x / 16) - (WINDOW_WIDTH / 2); +					if (x < 0) {x = 0;} else if (x + WINDOW_WIDTH > tilemap->width * 8) {x = tilemap->width * 8 - WINDOW_WIDTH;} +				} +				if (tilemap->height <= WINDOW_HEIGHT / 8) { +					y = -((WINDOW_HEIGHT - tilemap->height * 8) / 2); +				} else { +					y = (entities.player[0].y / 16) - (WINDOW_HEIGHT / 2); +					if (y < 0) {y = 0;} else if (y + WINDOW_HEIGHT > tilemap->height * 8) {y = tilemap->height * 8 - WINDOW_HEIGHT;} +				}  				game_render(framebuffer, x, y);  				game_render_flush(framebuffer); @@ -675,10 +692,18 @@ void main_loop(void) {  				if (fade == 255) {  					if (game_load_level(game_next_level)) {  						game_state = STATE_FADE_OUT; -						x = (entities.player[0].x / 16) - (WINDOW_WIDTH / 2); -						y = (entities.player[0].y / 16) - (WINDOW_HEIGHT / 2); -						if (x < 0) {x = 0;} else if (x + WINDOW_WIDTH > tilemap->width * 8) {x = tilemap->width * 8 - WINDOW_WIDTH;} -						if (y < 0) {y = 0;} else if (y + WINDOW_HEIGHT > tilemap->height * 8) {y = tilemap->height * 8 - WINDOW_HEIGHT;} +						if (tilemap->width < WINDOW_WIDTH / 8) { +							x = -((WINDOW_WIDTH - tilemap->width * 8) / 2); +						} else { +							x = (entities.player[0].x / 16) - (WINDOW_WIDTH / 2); +							if (x < 0) {x = 0;} else if (x + WINDOW_WIDTH > tilemap->width * 8) {x = tilemap->width * 8 - WINDOW_WIDTH;} +						} +						if (tilemap->height <= WINDOW_HEIGHT / 8) { +							y = -((WINDOW_HEIGHT - tilemap->height * 8) / 2); +						} else { +							y = (entities.player[0].y / 16) - (WINDOW_HEIGHT / 2); +							if (y < 0) {y = 0;} else if (y + WINDOW_HEIGHT > tilemap->height * 8) {y = tilemap->height * 8 - WINDOW_HEIGHT;} +						}  						game_render(framebuffer, x, y);  						SDL_SetRenderTarget(renderer, NULL); diff --git a/src/res/test.tmx b/src/res/test.tmx index 5ac87be..a449d81 100644 --- a/src/res/test.tmx +++ b/src/res/test.tmx @@ -43,8 +43,8 @@    <object id="5" name="tiny" type="warp" x="0" y="96" width="8" height="40">     <properties>      <property name="map" type="file" value="tiny.tmx"/> -    <property name="tox" type="int" value="96"/> -    <property name="toy" type="int" value="64"/> +    <property name="tox" type="int" value="48"/> +    <property name="toy" type="int" value="48"/>     </properties>    </object>    <object id="6" name="walky" type="walker" x="112" y="112"> diff --git a/src/res/tiny.tmx b/src/res/tiny.tmx index 6d593af..ece2481 100644 --- a/src/res/tiny.tmx +++ b/src/res/tiny.tmx @@ -1,16 +1,16 @@  <?xml version="1.0" encoding="UTF-8"?> -<map version="1.10" tiledversion="1.11.0" orientation="orthogonal" renderorder="right-down" width="21" height="13" tilewidth="8" tileheight="8" infinite="0" nextlayerid="5" nextobjectid="3"> +<map version="1.10" tiledversion="1.11.0" orientation="orthogonal" renderorder="right-down" width="11" height="9" tilewidth="8" tileheight="8" infinite="0" nextlayerid="5" nextobjectid="3">   <tileset firstgid="1" source="autotiles.tsx"/>   <tileset firstgid="33" source="tileset.tsx"/>   <objectgroup id="4" name="Object Layer 1"> -  <object id="1" name="test" type="warp" x="104" y="32" width="8" height="32"> +  <object id="1" name="test" type="warp" x="64" y="16" width="8" height="32">     <properties>      <property name="map" type="file" value="test.tmx"/>      <property name="tox" type="int" value="16"/>      <property name="toy" type="int" value="128"/>     </properties>    </object> -  <object id="2" name="lavender disk" type="save" x="88" y="64"> +  <object id="2" name="lavender disk" type="save" x="48" y="48">     <properties>      <property name="color" type="color" value="#ff9d56d0"/>     </properties> @@ -21,38 +21,30 @@    <properties>     <property name="middleground" type="bool" value="true"/>    </properties> -  <layer id="1" name="Tile Layer 1" width="21" height="13" offsetx="-4" offsety="-4"> +  <layer id="1" name="Tile Layer 1" width="11" height="9" offsetx="-4" offsety="-4">     <data encoding="csv"> -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,9,13,13,13,13,13,5,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,3,4,4,4,4,4,2,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,9,13,13,13,13,13,5,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,3,4,4,4,4,4,2,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 +0,0,0,0,0,0,0,0,0,0,0, +0,0,9,13,13,13,13,13,5,0,0, +0,0,3,4,4,4,4,4,2,0,0, +0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0, +0,0,9,13,13,13,13,13,5,0,0, +0,0,3,4,4,4,4,4,2,0,0, +0,0,0,0,0,0,0,0,0,0,0  </data>    </layer> -  <layer id="2" name="Tile Layer 2" width="21" height="13"> +  <layer id="2" name="Tile Layer 2" width="11" height="9">     <data encoding="csv"> -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,38,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,56,53,54,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 +0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,38,0,0,0, +0,0,56,53,54,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0  </data>    </layer>   </group> diff --git a/src/tilemap.c b/src/tilemap.c index 34e7c43..4819053 100644 --- a/src/tilemap.c +++ b/src/tilemap.c @@ -90,14 +90,36 @@ collision_T tilemap_area(struct tilemap *tilemap, int x1, int y1, int x2, int y2  void tilemap_background(struct tilemap *tilemap, int x, int y, int w, int h) {  	SDL_SetRenderDrawColor(renderer, tilemap->backdrop.r, tilemap->backdrop.g, tilemap->backdrop.b, tilemap->backdrop.a);  	SDL_RenderFillRect(renderer, &(SDL_Rect) {0, 0, w, h}); +	int bx = 0, by = 0, vw = w, vh = h; +	if (x < 0) { +		bx = -x; +		vw = w + x * 2; +		x = 0; +	} +	if (y < 0) { +		by = -y; +		vh = h + y * 2; +		y = 0; +	}  	for (int i = 0; i < tilemap->middleground; i++) { -		SDL_RenderCopy(renderer, tilemap->tilemaps[i], &(SDL_Rect) {x * tilemap->parallax[i].x, y * tilemap->parallax[i].y, w, h}, &(SDL_Rect) {0, 0, w, h}); +		SDL_RenderCopy(renderer, tilemap->tilemaps[i], &(SDL_Rect) {x * tilemap->parallax[i].x, y * tilemap->parallax[i].y, vw, vh}, &(SDL_Rect) {bx, by, vw, vh});  	}  }  void tilemap_foreground(struct tilemap *tilemap, int x, int y, int w, int h) { +	int bx = 0, by = 0, vw = w, vh = h; +	if (x < 0) { +		bx = -x; +		vw = w + x * 2; +		x = 0; +	} +	if (y < 0) { +		by = -y; +		vh = h + y * 2; +		y = 0; +	}  	for (int i = tilemap->middleground; i < tilemap->layers; i++) { -		SDL_RenderCopy(renderer, tilemap->tilemaps[i], &(SDL_Rect) {x * tilemap->parallax[i].x, y * tilemap->parallax[i].y, w, h}, &(SDL_Rect) {0, 0, w, h}); +		SDL_RenderCopy(renderer, tilemap->tilemaps[i], &(SDL_Rect) {x * tilemap->parallax[i].x, y * tilemap->parallax[i].y, vw, vh}, &(SDL_Rect) {bx, by, vw, vh});  	}  } | 
