309 lines
7.7 KiB
C++
309 lines
7.7 KiB
C++
#include "ascii.h"
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include "lua.h"
|
|
#include "font.c"
|
|
|
|
#define swap(a, b) {auto tmp=a;a=b;b=tmp;}
|
|
|
|
char window_title[256];
|
|
uint8_t char_screen[1200];
|
|
uint8_t color_screen[1200];
|
|
uint8_t screen_width = 40;
|
|
uint8_t screen_height = 30;
|
|
uint8_t current_color = 0x1e;
|
|
uint8_t cursor_x = 0;
|
|
uint8_t cursor_y = 0;
|
|
|
|
#define CHRSCR(x, y) char_screen[x+y*screen_width]
|
|
#define COLSCR(x, y) color_screen[x+y*screen_width]
|
|
|
|
SDL_Window *mini_win;
|
|
SDL_Renderer *mini_ren;
|
|
SDL_Texture *mini_bak;
|
|
Uint32 *pixels;
|
|
int pitch;
|
|
|
|
uint32_t palette[16] = { 0x00000000, 0x000000AA, 0x0000AA00, 0x0000AAAA, 0x00AA0000, 0x00AA00AA, 0x00AA5500, 0x00AAAAAA,
|
|
0x00555555, 0x005555FF, 0x0055FF55, 0x0055FFFF, 0x00FF5555, 0x00FF55FF, 0x00FFFF55, 0x00FFFFFF };
|
|
|
|
#define debug_line_size 40
|
|
#define debug_num_lines 30
|
|
#define debug_total_size 1200 //debug_line_size*debug_num_lines
|
|
char debug_text[debug_total_size];
|
|
int debug_cursor = 0;
|
|
|
|
//Uint8 keymapping[6] = { SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_Z, SDL_SCANCODE_X };
|
|
const Uint8 *keys;
|
|
Uint8 key_just_pressed = 0;
|
|
|
|
int mouse_x, mouse_y, mouse_wheel;
|
|
Uint32 mouse_buttons;
|
|
|
|
void reinit() {
|
|
screen_width = 40;
|
|
screen_height = 30;
|
|
current_color = 0x1e;
|
|
cursor_x = 0;
|
|
cursor_y = 0;
|
|
}
|
|
|
|
int main(int argc,char*argv[]) {
|
|
|
|
for (int i=0; i<debug_total_size;++i) debug_text[i] = 32;
|
|
|
|
SDL_Init(49);
|
|
mini_win = SDL_CreateWindow(window_title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
|
|
mini_ren = SDL_CreateRenderer(mini_win, -1, SDL_RENDERER_PRESENTVSYNC);
|
|
//SDL_CreateWindowAndRenderer(512,512,0,&mini_win,&mini_ren);
|
|
SDL_RenderSetLogicalSize(mini_ren, 320, 240);
|
|
mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 320, 240);
|
|
bool exit = false;
|
|
SDL_Event mini_eve;
|
|
|
|
reinit();
|
|
debug("ASCII SYSTEM BOOTING...");
|
|
lua_init();
|
|
lua_call_init();
|
|
|
|
while(!exit) {
|
|
key_just_pressed = 0;
|
|
mouse_wheel = 0;
|
|
while(SDL_PollEvent(&mini_eve)) {
|
|
if (mini_eve.type == SDL_QUIT) { exit=true; break; }
|
|
if (mini_eve.type == SDL_KEYDOWN) {
|
|
if (mini_eve.key.keysym.scancode == SDL_SCANCODE_ESCAPE) {
|
|
if (lua_is_playing()) {
|
|
lua_quit();
|
|
reinit();
|
|
} else {
|
|
lua_init();
|
|
lua_call_init();
|
|
}
|
|
} else if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F5) {
|
|
lua_quit();
|
|
reinit();
|
|
lua_init();
|
|
lua_call_init();
|
|
} else {
|
|
key_just_pressed = mini_eve.key.keysym.scancode;
|
|
}
|
|
}
|
|
if (mini_eve.type == SDL_MOUSEWHEEL) {
|
|
mouse_wheel = mini_eve.wheel.y;
|
|
}
|
|
}
|
|
keys = SDL_GetKeyboardState(NULL);
|
|
mouse_buttons = SDL_GetMouseState(&mouse_x, &mouse_y);
|
|
mouse_x /= 4; mouse_y /= 4;
|
|
|
|
if (lua_is_playing()) {
|
|
lua_call_update();
|
|
} else {
|
|
loop();
|
|
}
|
|
SDL_LockTexture(mini_bak, NULL, (void**)&pixels, &pitch);
|
|
|
|
for (int y=0; y<screen_height; ++y) {
|
|
for (int x=0; x<screen_width; ++x) {
|
|
const uint8_t chr_color = COLSCR(x,y);
|
|
const uint32_t ink_color = palette[chr_color & 0x0f];
|
|
const uint32_t paper_color = palette[chr_color >> 4];
|
|
const uint8_t chr = CHRSCR(x,y);
|
|
for (int l=0; l<8; ++l) {
|
|
const uint8_t line = font[chr*8+l];
|
|
for (int b=0; b<8; ++b) {
|
|
if ((line >> (7-b)) & 0x01) {
|
|
pixels[b+(x*8)+((y*8)+l)*(screen_width*8)] = ink_color;
|
|
} else {
|
|
pixels[b+(x*8)+((y*8)+l)*(screen_width*8)] = paper_color;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//for (int i=0;i<screen_surface->size;++i) pixels[i] = palette[screen_surface->p[i]];
|
|
|
|
SDL_UnlockTexture(mini_bak);
|
|
SDL_RenderCopy(mini_ren, mini_bak, NULL, NULL);
|
|
SDL_RenderPresent(mini_ren);
|
|
}
|
|
lua_quit();
|
|
SDL_Quit();
|
|
return 0;
|
|
}
|
|
|
|
void cls(uint8_t value) {
|
|
SDL_memset(char_screen, value, screen_width*screen_height);
|
|
SDL_memset(color_screen, current_color, screen_width*screen_height);
|
|
}
|
|
|
|
void ink(uint8_t value) {
|
|
current_color = (current_color & 0xf0) + (value & 0x0f);
|
|
}
|
|
|
|
void paper(uint8_t value) {
|
|
current_color = (current_color & 0x0f) + (value << 4);
|
|
}
|
|
|
|
void color(uint8_t ink, uint8_t paper) {
|
|
current_color = (ink & 0x0f) + (paper << 4);
|
|
}
|
|
|
|
void locate(uint8_t x, uint8_t y) {
|
|
cursor_x = min(x, screen_width-1);
|
|
cursor_y = min(y, screen_height-1);
|
|
}
|
|
|
|
void print(const char *str, int x, int y) {
|
|
if (x >= 0) cursor_x = min(x, screen_width-1);
|
|
if (y >= 0) cursor_y = min(y, screen_height-1);
|
|
int len = SDL_strlen(str);
|
|
if ((cursor_x+len) > screen_width) len -= ((cursor_x+len) - screen_width);
|
|
for (int i=0; i < len; ++i) {
|
|
CHRSCR(cursor_x+i, cursor_y) = str[i];
|
|
COLSCR(cursor_x+i, cursor_y) = current_color;
|
|
}
|
|
}
|
|
|
|
bool btn(uint8_t i) {
|
|
return keys[i];
|
|
}
|
|
|
|
bool btnp(uint8_t i) {
|
|
return key_just_pressed == i;
|
|
}
|
|
|
|
int mousex() {
|
|
return mouse_x;
|
|
}
|
|
|
|
int mousey() {
|
|
return mouse_y;
|
|
}
|
|
|
|
int mousewheel() {
|
|
return mouse_wheel;
|
|
}
|
|
|
|
bool mousebutton(uint8_t i) {
|
|
return mouse_buttons & SDL_BUTTON(i);
|
|
}
|
|
|
|
int time() {
|
|
return SDL_GetTicks();
|
|
}
|
|
|
|
/*float abs(float x) {
|
|
return SDL_fabsf(x);
|
|
}*/
|
|
|
|
float flr(float x) {
|
|
return SDL_floorf(x);
|
|
}
|
|
|
|
float sgn(float x) {
|
|
return x >= 0 ? 1 : -1;
|
|
}
|
|
|
|
#ifndef __LINUX__
|
|
|
|
float ceil(float x) {
|
|
return SDL_ceilf(x);
|
|
}
|
|
|
|
float sin(float x) {
|
|
return SDL_sinf(x);
|
|
}
|
|
|
|
float cos(float x) {
|
|
return SDL_cosf(x);
|
|
}
|
|
|
|
float atan2(float dx, float dy) {
|
|
return SDL_atan2f(dx, dy);
|
|
}
|
|
|
|
float sqrt(float x) {
|
|
return SDL_sqrtf(x);
|
|
}
|
|
|
|
#endif
|
|
|
|
float max(float x, float y) {
|
|
return SDL_max(x, y);
|
|
}
|
|
|
|
float mid(float x, float y, float z) {
|
|
return max(x, min(y, z));
|
|
}
|
|
|
|
float min(float x, float y) {
|
|
return SDL_min(x, y);
|
|
}
|
|
|
|
int rnd(int x) {
|
|
return rand()%x;
|
|
}
|
|
|
|
/*void srand(int x) {
|
|
srand(x);
|
|
}*/
|
|
|
|
char tostr_tmp[256];
|
|
const char* tostr(int val) {
|
|
return SDL_itoa(val, tostr_tmp, 10);
|
|
}
|
|
|
|
void debug_one_line_up() {
|
|
for (int i=0; i<debug_total_size-debug_line_size;++i) debug_text[i] = debug_text[i+debug_line_size];
|
|
for (int i=debug_total_size-debug_line_size; i<debug_total_size;++i) debug_text[i] = 32;
|
|
debug_cursor = debug_total_size-debug_line_size;
|
|
}
|
|
|
|
void debug(const char *str) {
|
|
const int len = SDL_strlen(str);
|
|
for (int i=0; i<len;++i) {
|
|
debug_text[debug_cursor++] = str[i];
|
|
if (debug_cursor >= debug_total_size) debug_one_line_up();
|
|
}
|
|
debug_cursor = (int(debug_cursor/debug_line_size)+1)*debug_line_size;
|
|
if (debug_cursor >= debug_total_size) debug_one_line_up();
|
|
}
|
|
|
|
void pdebug() {
|
|
int i=0;
|
|
for (int y=0; y<debug_num_lines;++y) {
|
|
for (int x=0; x<debug_line_size;++x) {
|
|
CHRSCR(x, y) = debug_text[i++];
|
|
COLSCR(x, y) = 0x1e;
|
|
}
|
|
}
|
|
}
|
|
|
|
uint8_t ascii(const char *str, uint8_t index) {
|
|
return str[index];
|
|
}
|
|
|
|
void setchar(uint8_t index, uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6, uint8_t b7) {
|
|
// [TODO]
|
|
}
|
|
|
|
uint8_t peek(uint16_t addr) {
|
|
// [TODO]
|
|
return 0;
|
|
}
|
|
|
|
void poke(uint16_t addr, uint8_t val) {
|
|
// [TODO]
|
|
}
|
|
|
|
void sound(float freq, uint32_t len) {
|
|
// [TODO]
|
|
}
|
|
|
|
void nosound() {
|
|
// [TODO]
|
|
}
|