1
0
Fork 0
2020-embedded-game-programming/src/main.c

105 lines
2.1 KiB
C
Raw Normal View History

2020-05-18 03:29:45 +00:00
#include "odroid/display.h"
2020-05-15 01:44:56 +00:00
#include "odroid/input.h"
2020-05-20 01:16:30 +00:00
#include "odroid/sdcard.h"
2020-05-18 03:29:45 +00:00
#include "macros.h"
2020-05-15 01:44:56 +00:00
#include <esp_log.h>
2020-05-10 02:07:57 +00:00
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
2020-05-18 03:29:45 +00:00
#include <string.h>
2020-05-15 01:44:56 +00:00
static const char* LOG_TAG = "Main";
2020-05-18 03:29:45 +00:00
static uint16_t gFramebuffer[LCD_WIDTH * LCD_HEIGHT];
2020-05-10 02:07:57 +00:00
void app_main(void)
{
2020-05-15 01:44:56 +00:00
Odroid_InitializeInput();
2020-05-18 03:29:45 +00:00
Odroid_InitializeDisplay();
2020-05-20 01:16:30 +00:00
Odroid_InitializeSdcard();
// Load sprite
uint16_t* sprite = (uint16_t*)malloc(64 * 64 * sizeof(uint16_t));
{
FILE* spriteFile = fopen("/sdcard/key", "r");
assert(spriteFile);
for (int i = 0; i < 64; ++i)
{
for (int j = 0; j < 64; ++j)
{
fread(sprite, sizeof(uint16_t), 64 * 64, spriteFile);
}
}
fclose(spriteFile);
}
2020-05-15 01:44:56 +00:00
ESP_LOGI(LOG_TAG, "Odroid initialization complete - entering main loop");
2020-05-18 03:29:45 +00:00
int x = 0;
int y = 0;
uint16_t color = 0xffff;
2020-05-10 02:07:57 +00:00
for (;;)
{
2020-05-18 03:29:45 +00:00
memset(gFramebuffer, 0, 320 * 240 * 2);
2020-05-15 01:44:56 +00:00
Odroid_Input input = Odroid_PollInput();
2020-05-18 03:29:45 +00:00
if (input.left) { x -= 20; }
else if (input.right) { x += 20; }
if (input.up) { y -= 20; }
else if (input.down) { y += 20; }
if (input.a) { color = SWAP_ENDIAN_16(RGB565(0xff, 0, 0)); }
else if (input.b) { color = SWAP_ENDIAN_16(RGB565(0, 0xff, 0)); }
else if (input.start) { color = SWAP_ENDIAN_16(RGB565(0, 0, 0xff)); }
else if (input.select) { color = SWAP_ENDIAN_16(RGB565(0xff, 0xff, 0xff)); }
2020-05-15 01:44:56 +00:00
2020-05-20 01:16:30 +00:00
int spriteRow = 0;
int spriteCol = 0;
for (int row = y; row < y + 64; ++row)
2020-05-18 03:29:45 +00:00
{
2020-05-20 01:16:30 +00:00
spriteCol = 0;
for (int col = x; col < x + 64; ++col)
2020-05-18 03:29:45 +00:00
{
2020-05-20 01:16:30 +00:00
uint16_t pixelColor = sprite[64 * spriteRow + spriteCol];
if (pixelColor != 0)
{
gFramebuffer[row * LCD_WIDTH + col] = color;
}
++spriteCol;
2020-05-18 03:29:45 +00:00
}
2020-05-20 01:16:30 +00:00
++spriteRow;
}
if (input.menu)
{
const char* snapFilename = "/sdcard/framebuf";
ESP_LOGI(LOG_TAG, "Writing snapshot to %s", snapFilename);
FILE* snapFile = fopen(snapFilename, "wb");
assert(snapFile);
fwrite(gFramebuffer, 1, LCD_WIDTH * LCD_HEIGHT * sizeof(gFramebuffer[0]), snapFile);
fclose(snapFile);
2020-05-18 03:29:45 +00:00
}
2020-05-15 01:44:56 +00:00
2020-05-18 03:29:45 +00:00
Odroid_DrawFrame(gFramebuffer);
2020-05-10 02:07:57 +00:00
}
// Should never get here
esp_restart();
}
2020-05-15 01:44:56 +00:00