312 lines
8.7 KiB
C
312 lines
8.7 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, 12, "../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;
|
|
}
|
|
|
|
SDL_Keycode ConvertKPToNonKP(SDL_Keycode keycode) {
|
|
switch (keycode) {
|
|
case SDLK_KP_0:
|
|
return SDLK_0;
|
|
case SDLK_KP_1:
|
|
return SDLK_1;
|
|
case SDLK_KP_2:
|
|
return SDLK_2;
|
|
case SDLK_KP_3:
|
|
return SDLK_3;
|
|
case SDLK_KP_4:
|
|
return SDLK_4;
|
|
case SDLK_KP_5:
|
|
return SDLK_5;
|
|
case SDLK_KP_6:
|
|
return SDLK_6;
|
|
case SDLK_KP_7:
|
|
return SDLK_7;
|
|
case SDLK_KP_8:
|
|
return SDLK_8;
|
|
case SDLK_KP_9:
|
|
return SDLK_9;
|
|
case SDLK_KP_PERIOD:
|
|
return SDLK_PERIOD;
|
|
case SDLK_KP_COMMA:
|
|
return SDLK_COMMA;
|
|
case SDLK_KP_DIVIDE:
|
|
return SDLK_SLASH;
|
|
case SDLK_KP_MULTIPLY:
|
|
return SDLK_ASTERISK;
|
|
case SDLK_KP_MINUS:
|
|
return SDLK_MINUS;
|
|
case SDLK_KP_PLUS:
|
|
return SDLK_PLUS;
|
|
case SDLK_KP_ENTER:
|
|
return SDLK_RETURN;
|
|
case SDLK_KP_EQUALS:
|
|
return SDLK_EQUALS;
|
|
case SDLK_KP_LEFTPAREN:
|
|
return SDLK_LEFTPAREN;
|
|
case SDLK_KP_RIGHTPAREN:
|
|
return SDLK_RIGHTPAREN;
|
|
case SDLK_KP_LEFTBRACE:
|
|
return SDLK_LEFTBRACKET;
|
|
case SDLK_KP_RIGHTBRACE:
|
|
return SDLK_RIGHTBRACKET;
|
|
case SDLK_KP_TAB:
|
|
return SDLK_TAB;
|
|
case SDLK_KP_BACKSPACE:
|
|
return SDLK_BACKSPACE;
|
|
case SDLK_KP_A:
|
|
return SDLK_a;
|
|
case SDLK_KP_B:
|
|
return SDLK_b;
|
|
case SDLK_KP_C:
|
|
return SDLK_c;
|
|
case SDLK_KP_D:
|
|
return SDLK_d;
|
|
case SDLK_KP_E:
|
|
return SDLK_e;
|
|
case SDLK_KP_F:
|
|
return SDLK_f;
|
|
case SDLK_KP_XOR:
|
|
return SDLK_CARET;
|
|
case SDLK_KP_PERCENT:
|
|
return SDLK_PERCENT;
|
|
case SDLK_KP_LESS:
|
|
return SDLK_LESS;
|
|
case SDLK_KP_GREATER:
|
|
return SDLK_GREATER;
|
|
case SDLK_KP_AMPERSAND:
|
|
return SDLK_AMPERSAND;
|
|
case SDLK_KP_DBLAMPERSAND:
|
|
return SDLK_AMPERSAND; // No direct match, best alternative
|
|
case SDLK_KP_VERTICALBAR:
|
|
return SDLK_BACKSLASH; // Vertical bar alternative
|
|
case SDLK_KP_DBLVERTICALBAR:
|
|
return SDLK_BACKSLASH; // No direct match
|
|
case SDLK_KP_COLON:
|
|
return SDLK_COLON;
|
|
case SDLK_KP_HASH:
|
|
return SDLK_HASH;
|
|
case SDLK_KP_SPACE:
|
|
return SDLK_SPACE;
|
|
case SDLK_KP_AT:
|
|
return SDLK_AT;
|
|
case SDLK_KP_EXCLAM:
|
|
return SDLK_EXCLAIM;
|
|
case SDLK_KP_MEMSTORE:
|
|
return SDLK_UNKNOWN; // No direct match
|
|
case SDLK_KP_MEMRECALL:
|
|
return SDLK_UNKNOWN;
|
|
case SDLK_KP_MEMCLEAR:
|
|
return SDLK_UNKNOWN;
|
|
case SDLK_KP_MEMADD:
|
|
return SDLK_PLUS;
|
|
case SDLK_KP_MEMSUBTRACT:
|
|
return SDLK_MINUS;
|
|
case SDLK_KP_MEMMULTIPLY:
|
|
return SDLK_ASTERISK;
|
|
case SDLK_KP_MEMDIVIDE:
|
|
return SDLK_SLASH;
|
|
case SDLK_KP_PLUSMINUS:
|
|
return SDLK_UNKNOWN;
|
|
case SDLK_KP_CLEAR:
|
|
return SDLK_UNKNOWN;
|
|
case SDLK_KP_CLEARENTRY:
|
|
return SDLK_UNKNOWN;
|
|
case SDLK_KP_BINARY:
|
|
return SDLK_UNKNOWN;
|
|
case SDLK_KP_OCTAL:
|
|
return SDLK_UNKNOWN;
|
|
case SDLK_KP_DECIMAL:
|
|
return SDLK_UNKNOWN;
|
|
case SDLK_KP_HEXADECIMAL:
|
|
return SDLK_UNKNOWN;
|
|
default:
|
|
return keycode; // If it's not a KP key, return it unchanged
|
|
}
|
|
}
|
|
|
|
|
|
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 = ConvertKPToNonKP(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, renderer);
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
if (activeEditor && !activeEditor->readOnly && activeEditor->cursor_pos < MAX_LINE_WIDTH) {
|
|
if (keySym >= 32 && keySym <= 126) {
|
|
if (keySym > 0x60 && keySym < 0x7b) {
|
|
keySym -= 0x20;
|
|
}
|
|
insert_character(activeEditor, (keySym & 0xff), renderer);
|
|
} else if (keySym == SDLK_BACKSPACE || keySym == SDLK_DELETE) {
|
|
remove_character(activeEditor, 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;
|
|
} |