Reset
This commit is contained in:
26
src/dawntermlinux/CMakeLists.txt
Normal file
26
src/dawntermlinux/CMakeLists.txt
Normal file
@ -0,0 +1,26 @@
|
||||
# Copyright (c) 2024 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Libraries
|
||||
target_link_libraries(${DAWN_TARGET_NAME}
|
||||
PUBLIC
|
||||
m
|
||||
)
|
||||
|
||||
# Includes
|
||||
target_include_directories(${DAWN_TARGET_NAME}
|
||||
PUBLIC
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
)
|
||||
|
||||
# Subdirs
|
||||
add_subdirectory(display)
|
||||
|
||||
# Sources
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
input.c
|
||||
linuxhost.c
|
||||
)
|
12
src/dawntermlinux/display/CMakeLists.txt
Normal file
12
src/dawntermlinux/display/CMakeLists.txt
Normal file
@ -0,0 +1,12 @@
|
||||
# Copyright (c) 2024 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Subdirs
|
||||
|
||||
# Sources
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
display.c
|
||||
)
|
78
src/dawntermlinux/display/display.c
Normal file
78
src/dawntermlinux/display/display.c
Normal file
@ -0,0 +1,78 @@
|
||||
/**
|
||||
* Copyright (c) 2024 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "display/display.h"
|
||||
#include "display/frame.h"
|
||||
#include "display/termcolor.h"
|
||||
#include "assert/assert.h"
|
||||
|
||||
void displayInit() {
|
||||
frameInit();
|
||||
}
|
||||
|
||||
void displayUpdate() {
|
||||
frameUpdate();
|
||||
|
||||
// Print clear and return to top left
|
||||
printf("\033[2J\033[1;1H");
|
||||
int32_t i = 0;
|
||||
const char_t *color;
|
||||
for(uint16_t y = 0; y < FRAME_HEIGHT; y++) {
|
||||
for(uint16_t x = 0; x < FRAME_WIDTH; x++) {
|
||||
switch(FRAME_COLOR[i]) {
|
||||
case COLOR_BLACK:
|
||||
color = TERM_COLOR_BLACK;
|
||||
break;
|
||||
|
||||
case COLOR_WHITE:
|
||||
color = TERM_COLOR_WHITE;
|
||||
break;
|
||||
|
||||
case COLOR_RED:
|
||||
color = TERM_COLOR_RED;
|
||||
break;
|
||||
|
||||
case COLOR_GREEN:
|
||||
color = TERM_COLOR_GREEN;
|
||||
break;
|
||||
|
||||
case COLOR_BLUE:
|
||||
color = TERM_COLOR_BLUE;
|
||||
break;
|
||||
|
||||
case COLOR_YELLOW:
|
||||
color = TERM_COLOR_YELLOW;
|
||||
break;
|
||||
|
||||
case COLOR_MAGENTA:
|
||||
color = TERM_COLOR_MAGENTA;
|
||||
break;
|
||||
|
||||
case COLOR_CYAN:
|
||||
color = TERM_COLOR_CYAN;
|
||||
break;
|
||||
|
||||
default:
|
||||
assertUnreachable("Invalid color.");
|
||||
}
|
||||
|
||||
printf(
|
||||
"%s%c",
|
||||
color,
|
||||
FRAME_BUFFER[i]
|
||||
);
|
||||
i++;
|
||||
}
|
||||
|
||||
printf("\r\n");
|
||||
}
|
||||
|
||||
printf(TERM_COLOR_RESET);
|
||||
}
|
||||
|
||||
void displayDispose() {
|
||||
}
|
20
src/dawntermlinux/display/termcolor.h
Normal file
20
src/dawntermlinux/display/termcolor.h
Normal file
@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Copyright (c) 2024 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "dawn.h"
|
||||
|
||||
#define TERM_COLOR_BLACK "\x1b[30m"
|
||||
#define TERM_COLOR_WHITE "\x1b[37m"
|
||||
#define TERM_COLOR_RED "\x1b[31m"
|
||||
#define TERM_COLOR_GREEN "\x1b[32m"
|
||||
#define TERM_COLOR_BLUE "\x1b[34m"
|
||||
#define TERM_COLOR_YELLOW "\x1b[33m"
|
||||
#define TERM_COLOR_MAGENTA "\x1b[35m"
|
||||
#define TERM_COLOR_CYAN "\x1b[36m"
|
||||
|
||||
#define TERM_COLOR_RESET "\x1b[0m"
|
38
src/dawntermlinux/input.c
Normal file
38
src/dawntermlinux/input.c
Normal file
@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Copyright (c) 2024 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "input.h"
|
||||
#include "linuxhost.h"
|
||||
|
||||
inputstate_t inputGetState(const inputbind_t bind) {
|
||||
int32_t k = LINUX_TERM_HOST_KEY_PRESS;
|
||||
|
||||
if(k == -1) return false;
|
||||
|
||||
switch(bind) {
|
||||
case INPUT_BIND_DOWN:
|
||||
return k == 's';
|
||||
|
||||
case INPUT_BIND_UP:
|
||||
return k == 'w';
|
||||
|
||||
case INPUT_BIND_LEFT:
|
||||
return k == 'a';
|
||||
|
||||
case INPUT_BIND_RIGHT:
|
||||
return k == 'd';
|
||||
|
||||
case INPUT_BIND_ACCEPT:
|
||||
return k == 'e';
|
||||
|
||||
case INPUT_BIND_CANCEL:
|
||||
return k == 'q';
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
72
src/dawntermlinux/linuxhost.c
Normal file
72
src/dawntermlinux/linuxhost.c
Normal file
@ -0,0 +1,72 @@
|
||||
/**
|
||||
* Copyright (c) 2024 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "linuxhost.h"
|
||||
#include "game.h"
|
||||
#include <unistd.h>
|
||||
#include <termios.h>
|
||||
|
||||
int32_t LINUX_TERM_HOST_KEY_PRESS;
|
||||
struct termios origionalTermios;
|
||||
|
||||
void terminalModeReset() {
|
||||
tcsetattr(0, TCSANOW, &origionalTermios);
|
||||
}
|
||||
|
||||
void terminalModeSetCONIO() {
|
||||
struct termios new_termios;
|
||||
|
||||
/* take two copies - one for now, one for later */
|
||||
tcgetattr(0, &origionalTermios);
|
||||
memcpy(&new_termios, &origionalTermios, sizeof(new_termios));
|
||||
|
||||
/* register cleanup handler, and set the new terminal mode */
|
||||
atexit(terminalModeReset);
|
||||
cfmakeraw(&new_termios);
|
||||
tcsetattr(0, TCSANOW, &new_termios);
|
||||
}
|
||||
|
||||
int32_t keyboardCharacterAvailable() {
|
||||
struct timeval tv = { 0L, 0L };
|
||||
fd_set fds;
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(0, &fds);
|
||||
return select(1, &fds, NULL, NULL, &tv) > 0;
|
||||
}
|
||||
|
||||
int32_t keyboardCharacterRead() {
|
||||
int32_t r;
|
||||
uchar_t c;
|
||||
if ((r = read(0, &c, sizeof(c))) < 0) {
|
||||
return r;
|
||||
} else {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
|
||||
int32_t linuxHostBeginUpdating() {
|
||||
while(true) {
|
||||
terminalModeSetCONIO();
|
||||
|
||||
while(!keyboardCharacterAvailable()) {
|
||||
uint8_t result = gameUpdate(0.1f);
|
||||
if(result == GAME_UPDATE_RESULT_EXIT) return 0;
|
||||
if(result != GAME_UPDATE_RESULT_CONTINUE) return -1;
|
||||
|
||||
// Clear input.
|
||||
LINUX_TERM_HOST_KEY_PRESS = -1;
|
||||
|
||||
// Clear console and return to left top
|
||||
printf("\033[2J\033[1;1H");
|
||||
usleep(100000);
|
||||
}
|
||||
|
||||
LINUX_TERM_HOST_KEY_PRESS = keyboardCharacterRead();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
16
src/dawntermlinux/linuxhost.h
Normal file
16
src/dawntermlinux/linuxhost.h
Normal file
@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Copyright (c) 2024 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "display/display.h"
|
||||
|
||||
extern int32_t LINUX_TERM_HOST_KEY_PRESS;
|
||||
|
||||
/**
|
||||
* Begins updating the host.
|
||||
*/
|
||||
int32_t linuxHostBeginUpdating();
|
Reference in New Issue
Block a user