MandAzur/src/main.cpp

273 lines
6.8 KiB
C++

#include <azur/azur.h>
#include <azur/gint/render.h>
#include <gint/drivers/r61524.h>
#include <gint/rtc.h>
#include <gint/keyboard.h>
#include <gint/kmalloc.h>
#include <libprof.h>
#include <gint/usb.h>
#include <gint/usb-ff-bulk.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fxlibc/printf.h>
#include <cstdint>
#include <num/num.h>
#include "utilities.h"
#include "fractalshaders.h"
bool screenshot = false;
bool record = false;
bool textoutput = false;
bool exitToOS = false;
int SCALE_PIXEL=2;
#define BIAS 1
#define NOBIAS (1-BIAS)
uint8_t texttodraw=2;
uint8_t Mandel_Julia_Switch = 1;
float Cr, Ci;
void do_nothing( void )
{
}
static void update( float dt )
{
}
static void hook_prefrag(int id, void *fragment, int size)
{
if(!screenshot && !record)
return;
/* Screenshot takes precedence */
char const *type = screenshot ? "image" : "video";
int pipe = usb_ff_bulk_output();
if(id == 0) {
usb_fxlink_header_t h;
usb_fxlink_image_t sh;
int size = azrp_width * azrp_height * 2;
usb_fxlink_fill_header(&h, "fxlink", type, size + sizeof sh);
sh.width = htole32(azrp_width);
sh.height = htole32(azrp_height);
sh.pixel_format = htole32(USB_FXLINK_IMAGE_RGB565);
usb_write_sync(pipe, &h, sizeof h, 4, false);
usb_write_sync(pipe, &sh, sizeof sh, 4, false);
}
usb_write_sync(pipe, fragment, size, 4, false);
if(id == azrp_frag_count - 1) {
usb_commit_sync(pipe);
screenshot = false;
}
}
static void get_inputs( void )
{
key_event_t ev;
while((ev = pollevent()).type != KEYEV_NONE)
{
}
if(keydown(KEY_EXIT)) {exitToOS = true; };
if(keydown(KEY_7)) {screenshot = true;};
if(keydown(KEY_8)) {record = true; };
if(keydown(KEY_9)) {record = false; };
if(keydown(KEY_DEL)) {textoutput = true;};
if(keydown(KEY_OPTN))
{
SCALE_PIXEL=1;
azrp_config_scale(SCALE_PIXEL);
azrp_shader_clear_configure();
azrp_shader_image_rgb16_configure();
azrp_shader_image_p8_configure();
azrp_shader_image_p4_configure();
azrp_hook_set_prefrag(hook_prefrag);
azrp_mandelbrot_init();
azrp_julia_init();
};
if(keydown(KEY_VARS))
{
SCALE_PIXEL=2;
azrp_config_scale(SCALE_PIXEL);
azrp_shader_clear_configure();
azrp_shader_image_rgb16_configure();
azrp_shader_image_p8_configure();
azrp_shader_image_p4_configure();
azrp_hook_set_prefrag(hook_prefrag);
azrp_mandelbrot_init();
azrp_julia_init();
};
if(Mandel_Julia_Switch==2)
{
if(keydown(KEY_SHIFT) && keydown(KEY_LEFT))
{
if(Cr>-2.0f) Cr-=0.002;
}
else if(keydown(KEY_SHIFT) && keydown(KEY_RIGHT))
{
if(Cr<2.0f) Cr+=0.002;
}
else if(keydown(KEY_SHIFT) && keydown(KEY_DOWN))
{
if(Ci>-2.0f) Ci-=0.002;
}
else if(keydown(KEY_SHIFT) && keydown(KEY_UP))
{
if(Ci<2.0f) Ci+=0.002;
}
}
if(keydown(KEY_F1)) {texttodraw=0;}
if(keydown(KEY_F2)) {texttodraw=1;}
if(keydown(KEY_F3)) {texttodraw=2;}
if(keydown(KEY_F4)) {texttodraw=3;}
if(keydown(KEY_F5)) {Mandel_Julia_Switch=1;} // switch to Julia Set
if(keydown(KEY_F6)) {Mandel_Julia_Switch=2;} // switch to Mandelbrot Set
}
int main(void)
{
Cr = -0.480f;
Ci = 0.600f;
exitToOS = false;
float elapsedTime = 0.0f;
kmalloc_arena_t *_uram = kmalloc_get_arena("_uram");
kmalloc_gint_stats_t *_uram_stats;
__printf_enable_fp();
__printf_enable_fixed();
azrp_config_scale(SCALE_PIXEL);
azrp_shader_clear_configure();
azrp_shader_image_rgb16_configure();
azrp_shader_image_p8_configure();
azrp_shader_image_p4_configure();
azrp_hook_set_prefrag(hook_prefrag);
azrp_mandelbrot_init();
azrp_julia_init();
usb_interface_t const *interfaces[] = { &usb_ff_bulk, NULL };
usb_open(interfaces, GINT_CALL_NULL);
prof_init();
prof_t perf_update, perf_render;
uint32_t time_update=0, time_render=0;
do
{
perf_update = prof_make();
prof_enter(perf_update);
{
// read inputs from the player
get_inputs( );
// update as per the time spend to do the loop
update( elapsedTime );
// update the RAM consumption status
_uram_stats = kmalloc_get_gint_stats(_uram);
}
prof_leave(perf_update);
time_update = prof_time(perf_update);
perf_render = prof_make();
prof_enter(perf_render);
{
// all the stuff to be rendered should be put here
azrp_clear( C_BLACK );
if (Mandel_Julia_Switch==1) azrp_mandelbrot();
else if(Mandel_Julia_Switch==2) azrp_julia(Cr,Ci);
else do_nothing();
if (texttodraw>=1) Azur_draw_text(1,01, " FPS = %.0f - Render = %.0f", (float) (1000000.0f / elapsedTime), (float) time_render / 1000.0f );
if (texttodraw>=1 && Mandel_Julia_Switch==2) Azur_draw_text(1,11, " Cr = %.3f - Ci = %.3f", Cr, Ci );
if (texttodraw>=2) Azur_draw_text(1,31, "Update = %.0f mc secs", (float) time_update );
if (texttodraw>=2) Azur_draw_text(1,41, "Render = %.0f mc secs", (float) time_render );
if (texttodraw>=2) Azur_draw_text(1,51, ">Total = %.3f ml secs", (float) elapsedTime / 1000.0f );
if (texttodraw>=2) Azur_draw_text(1,61, ">Total = %.0f seconds", (float) elapsedTime );
if (texttodraw>=3) Azur_draw_text(1,81, "Mem Used : %d", _uram_stats->used_memory );
if (texttodraw>=3) Azur_draw_text(1,91, "Mem Free : %d", _uram_stats->free_memory );
if (texttodraw>=3) Azur_draw_text(1,101, "Mem Peak Used : %d", _uram_stats->peak_used_memory );
azrp_update();
}
prof_leave(perf_render);
time_render = prof_time(perf_render);
elapsedTime = ((float) (time_update+time_render));
/*
if (screenshot && usb_is_open())
{
usb_fxlink_screenshot(false);
screenshot = false;
}
if(record && usb_is_open())
{
usb_fxlink_videocapture(false);
}
*/
if (textoutput && usb_is_open())
{
if (Mandel_Julia_Switch==1) azrp_mandelbrot_USBDEBUG( );
else if (Mandel_Julia_Switch==2) azrp_julia_USBDEBUG( );
else do_nothing( );
textoutput = false;
}
}
while (exitToOS==false);
azrp_mandelbrot_close( );
azrp_julia_close( );
prof_quit();
usb_close();
return 1;
}