From 2bc98ed29afcb79c9cdfab6f0940378e4f195280 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Tue, 19 Feb 2013 17:34:25 -0500 Subject: Limit surfaces to 8 bpp. Set keys and palettes. Also clean up main() a bit more. --- diff --git a/src/area.c b/src/area.c index 12cbc6c..f8857b2 100644 --- a/src/area.c +++ b/src/area.c @@ -26,11 +26,33 @@ area_new(struct map *map) static void blit_map_layers(struct area *area) { + SDL_Color colors[256]; + int i; + SDL_Rect dstrect; + + for (i = 0; i < 256; ++i) { + colors[i].r = (i % 8) * 36; + colors[i].g = (i % 64 / 8) * 36; + colors[i].b = (i / 64) * 85; + } + + dstrect.x = 0; + dstrect.y = 0; + dstrect.w = area->map->width * area->map->tilewidth; + dstrect.h = area->map->height * area->map->tileheight; + area->map_layers[AREA_LAYER_BOT] = SDL_CreateRGBSurface( SDL_HWSURFACE | SDL_SRCCOLORKEY, area->map->width * area->map->tilewidth, area->map->height * area->map->tileheight, - 32, 0, 0, 0, 0); + 8, 0, 0, 0, 0); + SDL_FillRect(area->map_layers[AREA_LAYER_BOT], &dstrect, + SDL_MapRGB(area->map_layers[AREA_LAYER_BOT]->format, + 0xFC, 0x00, 0xFF)); + SDL_SetColorKey(area->map_layers[AREA_LAYER_BOT], SDL_SRCCOLORKEY, + SDL_MapRGB(area->map_layers[AREA_LAYER_BOT]->format, + 0xFC, 0x00, 0xFF)); + SDL_SetColors(area->map_layers[AREA_LAYER_BOT], colors, 0, 256); blit_map_layer(area, MAP_LAYER_GROUND, AREA_LAYER_BOT); blit_map_layer(area, MAP_LAYER_OBJ_LOW, AREA_LAYER_BOT); @@ -38,14 +60,28 @@ blit_map_layers(struct area *area) SDL_HWSURFACE | SDL_SRCCOLORKEY, area->map->width * area->map->tilewidth, area->map->height * area->map->tileheight, - 32, 0, 0, 0, 0); + 8, 0, 0, 0, 0); + SDL_FillRect(area->map_layers[AREA_LAYER_MID], &dstrect, + SDL_MapRGB(area->map_layers[AREA_LAYER_MID]->format, + 0xFC, 0x00, 0xFF)); + SDL_SetColorKey(area->map_layers[AREA_LAYER_MID], SDL_SRCCOLORKEY, + SDL_MapRGB(area->map_layers[AREA_LAYER_MID]->format, + 0xFC, 0x00, 0xFF)); + SDL_SetColors(area->map_layers[AREA_LAYER_MID], colors, 0, 256); blit_map_layer(area, MAP_LAYER_OBJ_MID, AREA_LAYER_MID); area->map_layers[AREA_LAYER_TOP] = SDL_CreateRGBSurface( SDL_HWSURFACE | SDL_SRCCOLORKEY, area->map->width * area->map->tilewidth, area->map->height * area->map->tileheight, - 32, 0, 0, 0, 0); + 8, 0, 0, 0, 0); + SDL_FillRect(area->map_layers[AREA_LAYER_TOP], &dstrect, + SDL_MapRGB(area->map_layers[AREA_LAYER_TOP]->format, + 0xFC, 0x00, 0xFF)); + SDL_SetColorKey(area->map_layers[AREA_LAYER_TOP], SDL_SRCCOLORKEY, + SDL_MapRGB(area->map_layers[AREA_LAYER_TOP]->format, + 0xFC, 0x00, 0xFF)); + SDL_SetColors(area->map_layers[AREA_LAYER_TOP], colors, 0, 256); blit_map_layer(area, MAP_LAYER_OBJ_HIGH, AREA_LAYER_TOP); } @@ -60,7 +96,6 @@ blit_map_layer(struct area *area, enum map_layer map_layer, Uint32 mts_lastgid; SDL_Rect tilerect, layerrect; - SDL_SetColorKey(area->map_layers[area_layer], SDL_SRCCOLORKEY, 0); for (i = 0; i < area->map->width * area->map->height; ++i) { gid = area->map->layers[map_layer].tiles[i]; if (gid == 0) { @@ -138,7 +173,7 @@ render_area_to_surface(struct area *area, SDL_Rect *arearect, /* Fast fill with black to avoid artifacts in off-map pixels. */ /* NB: This is gray, at least temporarily. */ SDL_FillRect(surface, &dstrect, - SDL_MapRGB(surface->format, 63, 63, 63)); + SDL_MapRGB(surface->format, 0, 0, 0)); /* For demo */ tmprect.x = dstrect.x + 120 - 8; diff --git a/src/main.c b/src/main.c index 6a1100f..498bb26 100644 --- a/src/main.c +++ b/src/main.c @@ -13,6 +13,7 @@ render_area_to_viewport(a, vp); \ SDL_Flip(vp->screen); \ SDL_Delay(10); \ + ++i; \ } while (0) int @@ -20,41 +21,40 @@ main(void) { struct viewport *vp; struct map *map; - struct image *img; +#if 0 struct map_tileset *ts; struct map_exit *e; +#endif struct area *a; + SDL_Color colors[256]; + int i; + Uint32 start, end; int x, y; init(); - vp = init_viewport(240, 160, 32); + vp = init_viewport(240, 160, 8); map = map_get("data/forest1-8bit.tmx"); a = area_new(map); - debug("screen: bpp: %u, masks: 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x", - vp->screen->format->BitsPerPixel, - vp->screen->format->Rmask, vp->screen->format->Gmask, - vp->screen->format->Bmask, vp->screen->format->Amask); - img = img_png_get("../forest-6-layer-test_ground.png"); - debug("image: bpp: %u, masks: 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x", - img->image->format->BitsPerPixel, - img->image->format->Rmask, img->image->format->Gmask, - img->image->format->Bmask, img->image->format->Amask); - img_png_free(img); - img = img_png_get("data/tilesets/mountain_landscape_19-16.png"); - debug("image: bpp: %u, masks: 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x", - img->image->format->BitsPerPixel, - img->image->format->Rmask, img->image->format->Gmask, - img->image->format->Bmask, img->image->format->Amask); - img_png_free(img); - img = img_png_get("data/tilesets/mountain_landscape_19-16-8bit.png"); - debug("image: bpp: %u, masks: 0x%8.8x 0x%8.8x 0x%8.8x 0x%8.8x", - img->image->format->BitsPerPixel, - img->image->format->Rmask, img->image->format->Gmask, - img->image->format->Bmask, img->image->format->Amask); - img_png_free(img); + for (i = 0; i < 256; ++i) { + colors[i].r = (i % 8) * 36; + colors[i].g = (i % 64 / 8) * 36; + colors[i].b = (i / 64) * 85; + } + if (!SDL_SetPalette(vp->screen, SDL_LOGPAL, colors, 0, 256)) { + warn("Failed to set palette"); + } + for (i = 0; i < 256; ++i) { + colors[i].r = i; + colors[i].g = i; + colors[i].b = i; + } +/* if (!SDL_SetPalette(vp->screen, SDL_PHYSPAL, colors, 0, 256)) { + warn("Failed to set palette"); + }*/ +#if 0 debug("Map dimensions: %dx%d", map->width, map->height); for (ts = map->tilesets_head; ts != NULL; ts = ts->next) { debug("Tileset name: %s", ts->tileset->name); @@ -69,8 +69,11 @@ main(void) e->target_map->width, e->target_map->height, e->target_x_coord, e->target_y_coord); } +#endif /* Demo */ + i = 0; + start = SDL_GetTicks(); x = 19 * map->tilewidth ; y = 38 * map->tileheight; for (; y > 34 * map->tileheight; y -= 2) DEMO(); /* Up */ @@ -84,6 +87,8 @@ main(void) for (; y > 5 * map->tileheight; y -= 2) DEMO(); /* Up */ for (; x < 15 * map->tilewidth ; x += 2) DEMO(); /* Right */ for (; y > 1 * map->tileheight; y -= 2) DEMO(); /* Up */ + end = SDL_GetTicks(); + debug("Rendered %d frames in %d milliseconds", i, end - start); quit(0); diff --git a/src/resources/map.c b/src/resources/map.c index 0719a14..6c96650 100644 --- a/src/resources/map.c +++ b/src/resources/map.c @@ -461,6 +461,10 @@ tmx_tileset_el_start(void *pv, const char *name, const char **attr) } sprintf(path, "%s/%s", dirname, source); img = img_png_get(path); + /* TODO: Get the color key from the trans attribute. */ + SDL_SetColorKey(img->image, SDL_SRCCOLORKEY, + SDL_MapRGB(img->image->format, + 0xFC, 0x00, 0xFF)); free(source); } else if (ts->type == TILESET_TYPE_COLLISION) { ts->collision_tiles = malloc(ts->width * ts->height * diff --git a/src/viewport.c b/src/viewport.c index 1a6e9dc..078d37c 100644 --- a/src/viewport.c +++ b/src/viewport.c @@ -19,7 +19,7 @@ init_viewport(int width, int height, int bpp) debug("Setting video mode..."); vp->screen = SDL_SetVideoMode(width, height, bpp, - SDL_HWSURFACE | SDL_ANYFORMAT); + SDL_HWSURFACE | SDL_HWPALETTE); if (vp->screen == NULL) { err(1, "Failed to set video mode (%s)", SDL_GetError()); } -- cgit v0.9.1