Do some work on the CPU, assembler still needs update
This commit is contained in:
156
main.c
156
main.c
@@ -1,55 +1,32 @@
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <threads.h>
|
||||
#include "util/font.h"
|
||||
#include "assembler/assembler.h"
|
||||
|
||||
//Screen dimension constants
|
||||
const int SCREEN_WIDTH = 640;
|
||||
const int SCREEN_HEIGHT = 480;
|
||||
const int SCREEN_WIDTH = 1280;
|
||||
const int SCREEN_HEIGHT = 720;
|
||||
const int targetFPS = 60;
|
||||
const int delayNeeded = 1000 / targetFPS;
|
||||
|
||||
void renderText(SDL_Renderer *renderer, TTF_Font *font, char *string, uint16_t x, uint16_t y, uint8_t r, uint8_t g,
|
||||
uint8_t b, uint8_t a) {
|
||||
SDL_Texture *fontTempTex;
|
||||
SDL_Surface *fontTempSurf;
|
||||
SDL_Color color = {r, g, b, a};
|
||||
fontTempSurf = TTF_RenderText_Blended(font, string, color);
|
||||
fontTempTex = SDL_CreateTextureFromSurface(renderer, fontTempSurf);
|
||||
//The window we'll be rendering to
|
||||
SDL_Window *window = NULL;
|
||||
|
||||
int iW, iH;
|
||||
SDL_QueryTexture(fontTempTex, NULL, NULL, &iW, &iH);
|
||||
//The surface contained by the window
|
||||
SDL_Renderer *renderer = NULL;
|
||||
|
||||
SDL_Rect srcRect;
|
||||
srcRect.x = 0;
|
||||
srcRect.y = 0;
|
||||
srcRect.w = iW;
|
||||
srcRect.h = iH;
|
||||
SDL_Rect rect1;
|
||||
|
||||
SDL_Rect dstRect;
|
||||
dstRect.x = x;
|
||||
dstRect.y = y;
|
||||
dstRect.w = iW;
|
||||
dstRect.h = iH;
|
||||
BitmapFont smallFont;
|
||||
|
||||
SDL_RenderCopy(renderer, fontTempTex, &srcRect, &dstRect);
|
||||
|
||||
SDL_FreeSurface(fontTempSurf);
|
||||
SDL_DestroyTexture(fontTempTex);
|
||||
}
|
||||
|
||||
int main(int argc, char *args[]) {
|
||||
//The window we'll be rendering to
|
||||
SDL_Window *window = NULL;
|
||||
|
||||
//The surface contained by the window
|
||||
SDL_Renderer *renderer = NULL;
|
||||
|
||||
SDL_Rect rect1;
|
||||
|
||||
TTF_Font *gFont;
|
||||
CPU cpu;
|
||||
|
||||
char programString[65535];
|
||||
|
||||
int init() {
|
||||
//Initialize SDL
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
|
||||
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
@@ -60,55 +37,98 @@ int main(int argc, char *args[]) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
gFont = TTF_OpenFont("../rasterthingy.ttf", 50);
|
||||
if (gFont == NULL) {
|
||||
printf("Failed to load lazy font! SDL_ttf Error: %s\n", TTF_GetError());
|
||||
return 1;
|
||||
}
|
||||
|
||||
//Create window
|
||||
window = SDL_CreateWindow("SDLko", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH,
|
||||
SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
|
||||
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);
|
||||
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, 0);
|
||||
init_cpu(&cpu);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
char textTemp[12];
|
||||
sprintf(textTemp, "%d", rect1.x);
|
||||
|
||||
renderText(renderer, smallFont, textTemp, 100, 100);
|
||||
|
||||
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);
|
||||
}
|
||||
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 quit = false;
|
||||
bool running = true;
|
||||
Uint64 start;
|
||||
Uint64 end;
|
||||
while (!quit) {
|
||||
while (running) {
|
||||
start = SDL_GetTicks64();
|
||||
while (SDL_PollEvent(&e)) {
|
||||
if (e.type == SDL_QUIT) quit = true;
|
||||
running = processEvent(e);
|
||||
}
|
||||
|
||||
status = render();
|
||||
if (status) {
|
||||
return status;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
char textTemp[12];
|
||||
sprintf(textTemp, "%d", rect1.x);
|
||||
|
||||
renderText(renderer, gFont, textTemp, 100, 100, 255, 255, 255, 255);
|
||||
|
||||
SDL_RenderPresent(renderer);
|
||||
end = SDL_GetTicks64();
|
||||
SDL_Delay((1000 / 60) - (end - start));
|
||||
const unsigned long timeNeeded = end - start;
|
||||
if (timeNeeded < delayNeeded) {
|
||||
SDL_Delay(delayNeeded - timeNeeded);
|
||||
} else {
|
||||
printf("%lu", timeNeeded);
|
||||
}
|
||||
}
|
||||
|
||||
TTF_CloseFont(gFont);
|
||||
uint8_t *program;
|
||||
int program_size;
|
||||
|
||||
completePass(programString, &cpu);
|
||||
|
||||
step(&cpu);
|
||||
|
||||
//Destroy window
|
||||
SDL_DestroyWindow(window);
|
||||
|
Reference in New Issue
Block a user