PageRenderTime 43ms CodeModel.GetById 2ms app.highlight 38ms RepoModel.GetById 1ms app.codeStats 0ms

/src/MenuMiniMap.cpp

http://github.com/clintbellanger/flare
C++ | 110 lines | 65 code | 17 blank | 28 comment | 26 complexity | 7268ea59365a40a331e8061da3879e4f MD5 | raw file
  1/*
  2Copyright 2011 Clint Bellanger
  3
  4This file is part of FLARE.
  5
  6FLARE is free software: you can redistribute it and/or modify it under the terms
  7of the GNU General Public License as published by the Free Software Foundation,
  8either version 3 of the License, or (at your option) any later version.
  9
 10FLARE is distributed in the hope that it will be useful, but WITHOUT ANY
 11WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 12PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 13
 14You should have received a copy of the GNU General Public License along with
 15FLARE.  If not, see http://www.gnu.org/licenses/
 16*/
 17
 18/**
 19 * class MenuMiniMap
 20 */
 21
 22
 23#include "MenuMiniMap.h"
 24#include "SharedResources.h"
 25
 26MenuMiniMap::MenuMiniMap() {
 27	
 28	color_wall = SDL_MapRGB(screen->format, 128,128,128);
 29	color_obst = SDL_MapRGB(screen->format, 64,64,64);
 30	color_hero = SDL_MapRGB(screen->format, 255,255,255);
 31	
 32	map_center.x = VIEW_W - 64;
 33	map_center.y = 80;
 34	map_area.x = VIEW_W - 128;
 35	map_area.y = 16;
 36	map_area.w = map_area.h = 128;
 37
 38}
 39
 40/**
 41 * Render a top-down version of the map (90 deg angle)
 42 */
 43void MenuMiniMap::render(MapCollision *collider, Point hero_pos, int map_w, int map_h) {
 44	Point hero_tile;
 45	Point map_tile;
 46	hero_tile.x = hero_pos.x / UNITS_PER_TILE;
 47	hero_tile.y = hero_pos.y / UNITS_PER_TILE;
 48	for (int i=0; i<127; i++) {
 49		for (int j=0; j<127; j++) {
 50			map_tile.x = hero_tile.x + i - 64;
 51			map_tile.y = hero_tile.y + j - 64;
 52			if (map_tile.x >= 0 && map_tile.x < map_w && map_tile.y >= 0 && map_tile.y < map_h) {
 53				if (collider->colmap[map_tile.x][map_tile.y] == 1) {
 54					drawPixel(screen, VIEW_W - 128 + i, 16+j, color_wall);
 55				}
 56				else if (collider->colmap[map_tile.x][map_tile.y] == 2) {
 57					drawPixel(screen, VIEW_W - 128 + i, 16+j, color_obst);
 58				}
 59			}
 60		}
 61	}
 62	drawPixel(screen,VIEW_W-64,80,color_hero); // hero
 63	drawPixel(screen,VIEW_W-64-1,80,color_hero); // hero
 64	drawPixel(screen,VIEW_W-64+1,80,color_hero); // hero
 65	drawPixel(screen,VIEW_W-64,80-1,color_hero); // hero	
 66	drawPixel(screen,VIEW_W-64,80+1,color_hero); // hero
 67
 68}
 69
 70/**
 71 * Render an "isometric" version of the map (45 deg angle)
 72 */
 73void MenuMiniMap::renderIso(MapCollision *collider, Point hero_pos, int map_w, int map_h) {
 74	int tile_type;
 75	Point screen_pos;
 76	Uint32 draw_color;
 77	Point hero_tile;
 78	
 79	hero_tile.x = hero_pos.x / UNITS_PER_TILE;
 80	hero_tile.y = hero_pos.y / UNITS_PER_TILE;
 81	
 82	for (int j=0; j<map_h; j++) {
 83		for (int i=0; i<map_w; i++) {
 84		
 85			tile_type = collider->colmap[i][j];
 86
 87			// the hero, walls, and low obstacles show as different colors
 88			if (i == hero_tile.x && j == hero_tile.y) draw_color = color_hero;
 89			else if (tile_type == 1) draw_color = color_wall;
 90			else if (tile_type == 2) draw_color = color_obst;
 91			else continue; // not visible on mini-map
 92						
 93			// isometric transform
 94			screen_pos.x = (i - hero_tile.x) - (j - hero_tile.y) + map_center.x;
 95			screen_pos.y = (i - hero_tile.x) + (j - hero_tile.y) + map_center.y;
 96			
 97			// each tile is 2 pixels wide to mimic isometric view
 98			if (isWithin(map_area, screen_pos)) {
 99				drawPixel(screen, screen_pos.x, screen_pos.y, draw_color);
100			}
101			screen_pos.x++;
102			if (isWithin(map_area, screen_pos)) {
103				drawPixel(screen, screen_pos.x, screen_pos.y, draw_color);			
104			}
105		}
106	}
107}
108
109MenuMiniMap::~MenuMiniMap() {
110}