TIC-80-guile/src/world.c

115 lines
3.2 KiB
C
Raw Normal View History

2017-09-26 08:59:34 +02:00
// MIT License
// Copyright (c) 2017 Vadim Grigoruk @nesbox // grigoruk@gmail.com
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "world.h"
#include "map.h"
#define PREVIEW_SIZE (TIC80_WIDTH * TIC80_HEIGHT * TIC_PALETTE_BPP / BITS_IN_BYTE)
static void drawGrid(World* world)
{
Map* map = world->map;
2017-10-20 09:37:30 +02:00
u8 color = (tic_color_light_blue);
2017-09-26 08:59:34 +02:00
for(s32 c = 0; c < TIC80_WIDTH; c += TIC_MAP_SCREEN_WIDTH)
world->tic->api.line(world->tic, c, 0, c, TIC80_HEIGHT, color);
for(s32 r = 0; r < TIC80_HEIGHT; r += TIC_MAP_SCREEN_HEIGHT)
world->tic->api.line(world->tic, 0, r, TIC80_WIDTH, r, color);
world->tic->api.rect_border(world->tic, 0, 0, TIC80_WIDTH, TIC80_HEIGHT, color);
2018-02-01 17:34:42 +01:00
tic_rect rect = {0, 0, TIC80_WIDTH, TIC80_HEIGHT};
2017-09-26 08:59:34 +02:00
if(checkMousePos(&rect))
{
2018-02-06 20:15:56 +01:00
setCursor(tic_cursor_hand);
2017-09-26 08:59:34 +02:00
s32 mx = getMouseX();
s32 my = getMouseY();
2018-02-01 16:32:31 +01:00
if(checkMouseDown(&rect, tic_mouse_left))
2017-09-26 08:59:34 +02:00
{
map->scroll.x = (mx - TIC_MAP_SCREEN_WIDTH/2) * TIC_SPRITESIZE;
map->scroll.y = (my - TIC_MAP_SCREEN_HEIGHT/2) * TIC_SPRITESIZE;
}
2018-02-01 16:32:31 +01:00
if(checkMouseClick(&rect, tic_mouse_left))
2017-09-26 08:59:34 +02:00
setStudioMode(TIC_MAP_MODE);
}
world->tic->api.rect_border(world->tic, map->scroll.x / TIC_SPRITESIZE, map->scroll.y / TIC_SPRITESIZE,
2017-10-20 09:37:30 +02:00
TIC_MAP_SCREEN_WIDTH+1, TIC_MAP_SCREEN_HEIGHT+1, (tic_color_red));
2017-09-26 08:59:34 +02:00
}
static void tick(World* world)
{
2018-02-13 11:29:33 +01:00
if(keyWasPressed(tic_key_tab)) setStudioMode(TIC_MAP_MODE);
2017-09-26 08:59:34 +02:00
2018-02-06 20:15:56 +01:00
memcpy(&world->tic->ram.vram, world->preview, PREVIEW_SIZE);
2017-09-26 08:59:34 +02:00
drawGrid(world);
}
void initWorld(World* world, tic_mem* tic, Map* map)
{
if(!world->preview)
2018-02-06 20:15:56 +01:00
world->preview = malloc(PREVIEW_SIZE);
2017-09-26 08:59:34 +02:00
*world = (World)
{
.tic = tic,
.map = map,
.tick = tick,
.preview = world->preview,
};
2018-02-06 20:15:56 +01:00
memset(world->preview, 0, PREVIEW_SIZE);
2017-09-26 08:59:34 +02:00
s32 colors[TIC_PALETTE_SIZE];
for(s32 i = 0; i < TIC80_WIDTH * TIC80_HEIGHT; i++)
{
2017-12-13 21:24:31 +01:00
u8 index = getBankMap()->data[i];
2017-09-26 08:59:34 +02:00
if(index)
{
2018-02-06 20:15:56 +01:00
memset(colors, 0, sizeof colors);
2017-09-26 08:59:34 +02:00
2017-12-13 21:24:31 +01:00
tic_tile* tile = &getBankTiles()->data[index];
2017-09-26 08:59:34 +02:00
for(s32 p = 0; p < TIC_SPRITESIZE * TIC_SPRITESIZE; p++)
{
u8 color = tic_tool_peek4(tile, p);
if(color)
colors[color]++;
}
s32 max = 0;
2018-02-06 20:15:56 +01:00
for(s32 c = 0; c < COUNT_OF(colors); c++)
2017-09-26 08:59:34 +02:00
if(colors[c] > colors[max]) max = c;
tic_tool_poke4(world->preview, i, max);
}
}
}