188 lines
4.9 KiB
C
188 lines
4.9 KiB
C
#include <SDL2/SDL.h>
|
|
#include <stdio.h>
|
|
#include <threads.h>
|
|
#include "util/font.h"
|
|
#include "assembler/assembler.h"
|
|
#include "util/texteditor.h"
|
|
|
|
//Screen dimension constants
|
|
const int SCREEN_WIDTH = 1280;
|
|
const int SCREEN_HEIGHT = 720;
|
|
const int targetFPS = 60;
|
|
const int delayNeeded = 1000 / targetFPS;
|
|
|
|
//The window we'll be rendering to
|
|
SDL_Window *window = NULL;
|
|
|
|
//The surface contained by the window
|
|
SDL_Renderer *renderer = NULL;
|
|
|
|
BitmapFont smallFont;
|
|
|
|
CPU cpu;
|
|
|
|
TextEditor codeEditor;
|
|
|
|
TextEditor *activeEditor;
|
|
|
|
char programString[65535];
|
|
|
|
int init() {
|
|
|
|
|
|
//Initialize SDL
|
|
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
|
|
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
|
|
return 1;
|
|
}
|
|
|
|
//Initialize SDL_ttf
|
|
if (TTF_Init() == -1) {
|
|
printf("SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError());
|
|
return 1;
|
|
}
|
|
|
|
|
|
//Create window
|
|
window = SDL_CreateWindow("SDLko", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH,
|
|
SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
|
|
if (window == NULL) {
|
|
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
|
|
return 1;
|
|
}
|
|
//Get window surface
|
|
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
|
if (renderer == NULL) {
|
|
printf("Renderer could not be created SDL_Error: %s\n", SDL_GetError());
|
|
return 1;
|
|
}
|
|
smallFont = prepText(renderer, 10, "../PublicPixel.ttf", 255, 255, 255, 255);
|
|
init_editor(&codeEditor, &smallFont, 50, 50, renderer);
|
|
activeEditor = &codeEditor;
|
|
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
|
|
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, 0);
|
|
generate_string_display(&codeEditor, renderer);
|
|
init_cpu(&cpu);
|
|
|
|
return 0;
|
|
}
|
|
|
|
SDL_Rect rect1;
|
|
SDL_Rect rect2;
|
|
|
|
int render() {
|
|
SDL_SetRenderDrawColor(renderer, 128, 0, 0, 255);
|
|
SDL_RenderClear(renderer);
|
|
|
|
SDL_SetRenderDrawColor(renderer, 0, 128, 0, 255);
|
|
rect1.x = (rect1.x + 1) % 400;
|
|
rect1.y = 10;
|
|
rect1.w = 50;
|
|
rect1.h = 10;
|
|
SDL_RenderFillRect(renderer, &rect1);
|
|
|
|
editor_render(&codeEditor, renderer);
|
|
|
|
SDL_RenderPresent(renderer);
|
|
return 0;
|
|
}
|
|
|
|
int processEvent(SDL_Event e) {
|
|
if (e.type == SDL_QUIT) { return 0; }
|
|
else if (e.type == SDL_WINDOWEVENT && e.window.event == SDL_WINDOWEVENT_RESIZED) {
|
|
int newWidth = e.window.data1;
|
|
int newHeight = e.window.data2;
|
|
|
|
// Adjust the viewport to match the new window size;
|
|
SDL_Rect viewport = {0, 0, newWidth, newHeight};
|
|
SDL_RenderSetViewport(renderer, &viewport);
|
|
} else if (e.type == SDL_KEYDOWN) {
|
|
int keySym = e.key.keysym.sym;
|
|
switch (keySym) {
|
|
case SDLK_UP:
|
|
if (activeEditor) {
|
|
move_cursor_relative(activeEditor, -1, 0);
|
|
}
|
|
break;
|
|
case SDLK_DOWN:
|
|
if (activeEditor) {
|
|
move_cursor_relative(activeEditor, 1, 0);
|
|
}
|
|
break;
|
|
case SDLK_LEFT:
|
|
if (activeEditor) {
|
|
move_cursor_relative(activeEditor, 0, -1);
|
|
}
|
|
break;
|
|
case SDLK_RIGHT:
|
|
if (activeEditor) {
|
|
move_cursor_relative(activeEditor, 0, 1);
|
|
}
|
|
break;
|
|
case SDLK_RETURN:
|
|
case SDLK_RETURN2:
|
|
if (activeEditor && !activeEditor->readOnly) {
|
|
insert_line_rel(activeEditor);
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
if (activeEditor && !activeEditor->readOnly) {
|
|
if (keySym >= 32 && keySym <= 126) {
|
|
if (keySym > 0x60 && keySym < 0x7b) {
|
|
keySym -= 0x20;
|
|
}
|
|
insert_character(activeEditor, (keySym & 0xff), renderer);
|
|
}
|
|
}
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
int main(__attribute__((unused)) int argc, __attribute__((unused)) char *args[]) {
|
|
int status = init();
|
|
if (status) {
|
|
return status;
|
|
}
|
|
|
|
//Hack to get window to stay up
|
|
SDL_Event e;
|
|
bool running = true;
|
|
Uint64 start;
|
|
Uint64 end;
|
|
while (running) {
|
|
start = SDL_GetTicks64();
|
|
while (SDL_PollEvent(&e)) {
|
|
running = processEvent(e);
|
|
}
|
|
|
|
status = render();
|
|
if (status) {
|
|
return status;
|
|
}
|
|
|
|
end = SDL_GetTicks64();
|
|
const unsigned long timeNeeded = end - start;
|
|
if (timeNeeded < delayNeeded) {
|
|
SDL_Delay(delayNeeded - timeNeeded);
|
|
} else {
|
|
printf("%lu", timeNeeded);
|
|
}
|
|
}
|
|
|
|
uint8_t *program;
|
|
int program_size;
|
|
|
|
completePass(programString, &cpu, true);
|
|
|
|
step(&cpu);
|
|
|
|
//Destroy window
|
|
SDL_DestroyWindow(window);
|
|
|
|
//Quit SDL subsystems
|
|
SDL_Quit();
|
|
|
|
return 0;
|
|
} |