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

165 lines
3.4 KiB
C

#include "odroid/audio.h"
#include "odroid/battery.h"
#include "odroid/display.h"
#include "odroid/input.h"
#include "odroid/sdcard.h"
#include "macros.h"
#include "text.h"
#include <esp_log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <string.h>
static const char* LOG_TAG = "Main";
static uint16_t gFramebuffer[LCD_WIDTH * LCD_HEIGHT];
void app_main(void)
{
Odroid_InitializeInput();
Odroid_InitializeDisplay();
Odroid_InitializeSdcard();
Odroid_InitializeBatteryReader();
Odroid_InitializeAudio();
// 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);
}
// Load sound effect
uint16_t* soundBuffer;
int soundEffectLength = 1441;
{
FILE* soundFile = fopen("/sdcard/jump", "r");
assert(soundFile);
uint8_t* soundEffect = malloc(soundEffectLength);
assert(soundEffect);
soundBuffer = malloc(soundEffectLength*2);
assert(soundBuffer);
fread(soundEffect, soundEffectLength, 1, soundFile);
for (int i = 0; i < soundEffectLength; ++i)
{
// 16 bits required but only MSB is actually sent to the DAC
soundBuffer[i] = (soundEffect[i] << 8u);
}
}
ESP_LOGI(LOG_TAG, "Odroid initialization complete - entering main loop");
int x = 0;
int y = 0;
uint16_t color = 0xffff;
int lastState = 0;
for (;;)
{
memset(gFramebuffer, 0, 320 * 240 * 2);
Odroid_Input input = Odroid_PollInput();
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)); }
int thisState = input.volume;
if ((thisState == 1) && (thisState != lastState))
{
Odroid_PlayAudio(soundBuffer, soundEffectLength*2);
}
lastState = thisState;
DrawText(gFramebuffer, "The Quick Brown Fox", 19, 0, 5, SWAP_ENDIAN_16(RGB565(0xFF, 0, 0)));
DrawText(gFramebuffer, "Jumped Over The:", 16, 0, 6, SWAP_ENDIAN_16(RGB565(0, 0xFF, 0)));
DrawText(gFramebuffer, "Lazy Dog?!", 10, 0, 7, SWAP_ENDIAN_16(RGB565(0, 0, 0xFF)));
int spriteRow = 0;
int spriteCol = 0;
for (int row = y; row < y + 64; ++row)
{
spriteCol = 0;
for (int col = x; col < x + 64; ++col)
{
uint16_t pixelColor = sprite[64 * spriteRow + spriteCol];
if (pixelColor != 0)
{
gFramebuffer[row * LCD_WIDTH + col] = color;
}
++spriteCol;
}
++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);
}
uint32_t batteryLevel = Odroid_ReadBatteryLevel();
ESP_LOGI(LOG_TAG, "Battery level: %u\n", batteryLevel);
if (batteryLevel < 3600)
{
Odroid_EnableBatteryLight();
}
else
{
Odroid_DisableBatteryLight();
}
Odroid_DrawFrame(gFramebuffer);
}
// Should never get here
esp_restart();
}