Created
November 24, 2024 22:46
-
-
Save celsowm/90546eed890f2f28009eebc25f1bd67f to your computer and use it in GitHub Desktop.
drawLine in SGDK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <genesis.h> | |
// Função genérica para desenhar uma linha entre dois pontos | |
void drawLine(u16 x0, u16 y0, u16 x1, u16 y1, u16 paletteIndex, u16 color, u8 tileSize) | |
{ | |
// Cria dinamicamente um tile com base no tamanho configurável | |
u32 lineTile[64]; // Máximo de 64 linhas (8x8 pixels no máximo) | |
for (int i = 0; i < tileSize; i++) | |
{ | |
// Cada linha do tile terá todos os bits preenchidos com '1' (para linhas sólidas) | |
lineTile[i] = (1 << tileSize) - 1; | |
} | |
// Carrega o tile na VRAM | |
u16 tileIndex = TILE_USER_INDEX; // Índice inicial para tiles customizados | |
VDP_loadTileData((const u32 *)lineTile, tileIndex, 1, DMA); | |
// Define a cor na paleta | |
VDP_setPaletteColor(1, RGB24_TO_VDPCOLOR(color)); | |
// Algoritmo de Bresenham para desenhar a linha | |
int dx = abs(x1 - x0); | |
int dy = abs(y1 - y0); | |
int sx = (x0 < x1) ? 1 : -1; | |
int sy = (y0 < y1) ? 1 : -1; | |
int err = dx - dy; | |
while (1) | |
{ | |
// Desenha um tile na posição atual | |
for (u8 ty = 0; ty < tileSize / 8; ty++) // Adiciona suporte para largura configurável | |
{ | |
for (u8 tx = 0; tx < tileSize / 8; tx++) | |
{ | |
VDP_setTileMapXY(BG_A, TILE_ATTR_FULL(paletteIndex, 0, 0, 0, tileIndex), x0 + tx, y0 + ty); | |
} | |
} | |
// Verifica se chegou ao destino | |
if (x0 == x1 && y0 == y1) | |
break; | |
// Calcula o próximo ponto | |
int e2 = 2 * err; | |
if (e2 > -dy) | |
{ | |
err -= dy; | |
x0 += sx; | |
} | |
if (e2 < dx) | |
{ | |
err += dx; | |
y0 += sy; | |
} | |
} | |
} | |
int main() | |
{ | |
// Inicializa o SGDK | |
VDP_init(); | |
// Define a cor de fundo | |
VDP_setPaletteColor(0, RGB24_TO_VDPCOLOR(0x000000)); | |
// Desenha uma linha diagonal branca com tamanho de tile 8x8 | |
drawLine(5, 5, 15, 15, PAL0, 0xFFFFFF, 8); // Linha branca de (5,5) até (15,15) | |
// Desenha uma linha horizontal vermelha com tamanho de tile 16x16 | |
drawLine(10, 20, 30, 20, PAL0, 0xFF0000, 16); // Linha vermelha de (10,20) até (30,20) | |
// Desenha uma linha vertical azul com tamanho de tile 32x32 | |
drawLine(20, 10, 20, 25, PAL0, 0x0000FF, 32); // Linha azul de (20,10) até (20,25) | |
while (1) | |
{ | |
SYS_doVBlankProcess(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment