Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] WASM #15

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ endif()

project ("sdl2-vga-terminal" VERSION 0.1 DESCRIPTION "Vga Terminal on SDL2")

option(WITH_EXAMPLES "build examples" ON)
option(WITH_TEST "build test" ON)
option(TEST_DUMP_SNAPSHOT "running test dumping the expected output instead of verifing it" OFF)
option(WITH_SNAPSHOT "running snapshot test" OFF)
option(WITH_SDL2_STATIC "linking STATIC LIB with SDL2 STATIC" OFF)


enable_testing()
# Include sub-projects.
add_subdirectory ("sdl2-vga-terminal")
40 changes: 17 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@

| Windows (x86, x64) |
|---------|
|![![Build status](https://ci.appveyor.com/api/projects/status/67mildjynhnlekk5/branch/master?svg=true)](https://ci.appveyor.com/project/Raffaello/sdl2-vga-terminal/branch/master)|
|[![Build status](https://ci.appveyor.com/api/projects/status/67mildjynhnlekk5/branch/master?svg=true)](https://ci.appveyor.com/project/Raffaello/sdl2-vga-terminal/branch/master)|

It is just a VGA font terminal using SDL2.

the VGA fonts are related to a `vgabios` project (http://savannah.nongnu.org/projects/vgabios/).
the VGA fonts are related to [vgabios](http://savannah.nongnu.org/projects/vgabios/) project.

At the moment support only mode 3.

It should be on 16 colors in the classic way, but can support more thanks to SDL2.

It is just a matter of fonts and a terminal grid for displaying texts.


## Compiling

Use `vcpkg` and install `SDL2`.
Expand All @@ -28,15 +27,12 @@ SDL2 Video sub-system has to be initialized before using `VgaTerminal` class.
for extended ASCII chars to use as literal (eg: char 205), it is required to save the source code with
the codepage 437 encoding (OEM United States / Extended ASCII / DOS Terminal).


There is a demo usage as part of this cmake compilation too.

There are few examples usage as part of this cmake compilation too.

## Screenshot

This screenshot is also used in the snapshot test.


![alt text](./sdl2-vga-terminal/test/snapshot/VgaTerminal.Snapshot.png "Title")

## Testing
Expand All @@ -49,25 +45,23 @@ It could be a little bit risky, but it is a way to have generated expected resul

The test suite take advantages of the option and will be compiled accordingly based on "dumping" or "testing".

please note if you are dumping the snapshot, you have to copy back to the `test/snapshot` directory to make the usable.
please note if you are dumping the snapshot, you have to copy back to the `test/snapshot` directory to make them usable.

The filename generated are based on the test that are running, ideally: `[Test-suite.Test-name].png`

but doesn't really matter can be used any name, it all depends how the test is written of course.
Just as a convention.

## TODO

Some must and ides:

- clear with bgCol, and only viewport.
- complete the cursor functionality (When typing show the cursor not blinking (reset timer?)
- CI with Azure Devops, travis, appveyor, sonarcloud lgtm, etc...
- decouple vga modes
- DAC Palette changing
- Cursor display
- doxygen / document the code
- use streams
- read
- unicode VGA font

- some functions can be optimized using SDL_Surface functions/SDL functions
Some must and ideas:

- web assembly example,build experimentation?
- add tests for the cmake configuration and install files, expect them to be "there".
- clear with bgCol, and only viewport.
- decouple vga modes
- DAC Palette changing
- doxygen / document the code
- use streams
- read
- unicode VGA font
- some functions can be optimized using SDL_Surface functions/SDL functions
10 changes: 10 additions & 0 deletions build_wasm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#/bin/bash

### More as a reminder to build it.

mkdir build
cd build
## emcc need to be in the path. (run source emcsdk_env.sh)
emcc -std=c++17 ../sdl2-vga-terminal/examples/wasm.cpp ../sdl2-vga-terminal/src/Window.cpp ../sdl2-vga-terminal/src/VgaTerminal.cpp -I ../sdl2-vga-terminal/src/ -s USE_SDL=2 -o v.html
python -m http.server 8080

108 changes: 68 additions & 40 deletions sdl2-vga-terminal/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,84 +1,112 @@
cmake_minimum_required (VERSION 3.8)

set (CMAKE_CXX_STANDARD 17)
set (CMAKE_C_STANDARD 11)
set (CMAKE_DEBUG_POSTFIX d)

find_package(SDL2 CONFIG REQUIRED)


############################ Shared lib ######################################
include(GenerateExportHeader)
add_library(vga-terminal-lib SHARED)
add_library(vga-terminal SHARED)
generate_export_header(
vga-terminal-lib
BASE_NAME vga-terminal-lib
vga-terminal
BASE_NAME vga-terminal
EXPORT_MACRO_NAME VGA_TERMINAL_EXPORT
EXPORT_FILE_NAME vga-terminal-export.h
STATIC_DEFINE vga-terminal-lib_BUILD_AS_STATIC
)
install(
TARGETS vga-terminal-lib
EXPORT vga-terminal-lib-config
TARGETS vga-terminal
EXPORT vga-terminal-config
DESTINATION lib
PUBLIC_HEADER DESTINATION inc
PUBLIC_HEADER DESTINATION include
)
install(FILES
${CMAKE_CURRENT_SOURCE_DIR}/include/vga-terminal.h
$<TARGET_FILE_DIR:vga-terminal-lib>/vga-terminal-export.h
DESTINATION inc
$<TARGET_FILE_DIR:vga-terminal>/vga-terminal-export.h
DESTINATION include
)
install(FILES
$<TARGET_FILE:SDL2::SDL2>
DESTINATION lib
)
export(TARGETS vga-terminal-lib
export(TARGETS vga-terminal
FILE ${CMAKE_BINARY_DIR}/vga-terminal.cmake
)
install(EXPORT
vga-terminal-lib-config DESTINATION cmake
vga-terminal-config DESTINATION cmake
)
target_include_directories(vga-terminal-lib PUBLIC
$<BUILD_INTERFACE:$<TARGET_FILE_DIR:vga-terminal-lib>>
target_include_directories(vga-terminal PUBLIC
$<BUILD_INTERFACE:$<TARGET_FILE_DIR:vga-terminal>>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
target_sources(vga-terminal-lib PRIVATE
target_sources(vga-terminal PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/Window.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/VgaTerminal.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/vga-terminal.cpp
)
target_link_libraries(vga-terminal-lib SDL2::SDL2 SDL2::SDL2main)
target_link_libraries(vga-terminal SDL2::SDL2 SDL2::SDL2main)


################ Shared code among exe and tests #############################
add_library(vga-terminal OBJECT)
target_sources(vga-terminal PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/VgaTerminal.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Window.cpp
########################### Static Lib #######################################
add_library(vga-terminal-static STATIC)
install(
TARGETS vga-terminal-static
EXPORT vga-terminal-static-config
DESTINATION static/lib
PUBLIC_HEADER DESTINATION static/include
)
install(FILES
${CMAKE_CURRENT_SOURCE_DIR}/src/VgaTerminal.hpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Window.hpp
DESTINATION static/include
)

target_include_directories(vga-terminal PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_link_libraries(vga-terminal PUBLIC SDL2::SDL2 SDL2::SDL2main)

export(TARGETS vga-terminal-static
FILE ${CMAKE_BINARY_DIR}/vga-terminal-static.cmake
)
install(EXPORT
vga-terminal-static-config DESTINATION static/cmake
)
target_include_directories(vga-terminal-static PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
$<INSTALL_INTERFACE:static/include>
)
target_sources(vga-terminal-static PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/Window.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/VgaTerminal.cpp
)
target_link_libraries(vga-terminal-static SDL2::SDL2 SDL2::SDL2main)

############################## Main Example ##################################
add_executable (${PROJECT_NAME} "sdl2-vga-terminal.cpp")
add_dependencies(${PROJECT_NAME} vga-terminal)
target_link_libraries(${PROJECT_NAME} vga-terminal)

################ Shared code among exe and tests #############################
add_library(vga-terminal-obj OBJECT)
target_sources(vga-terminal-obj PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/VgaTerminal.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Window.cpp
)

################## C example using SO/DLL with lazy loading ##################
add_executable (${PROJECT_NAME}-so "sdl2-vga-terminal-so.c")
add_dependencies(${PROJECT_NAME}-so vga-terminal-lib)
target_link_libraries(${PROJECT_NAME}-so vga-terminal-lib)
if(MSVC)
target_link_options(${PROJECT_NAME}-so PRIVATE
"/INCREMENTAL"
"/DELAYLOAD:$<TARGET_FILE_NAME:vga-terminal-lib>"
"/DELAY:UNLOAD"
target_include_directories(vga-terminal-obj PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
if(WITH_SDL2_STATIC)
target_link_libraries(vga-terminal-obj PUBLIC SDL2::SDL2-static SDL2::SDL2main)
else()
install(FILES
$<TARGET_FILE:SDL2::SDL2>
DESTINATION static/lib
)
target_link_libraries(vga-terminal-obj PUBLIC SDL2::SDL2 SDL2::SDL2main)
endif()


################## C example using SO/DLL run-time loading ##################
add_executable (${PROJECT_NAME}-load-so "sdl2-vga-terminal-load-so.c")
target_link_libraries(${PROJECT_NAME}-load-so PUBLIC SDL2::SDL2 SDL2::SDL2main)
### Examples
if(WITH_EXAMPLES)
add_subdirectory("examples")
endif()


############################### tests
############################### tests ########################################
if(WITH_TEST)
add_subdirectory("test")
endif()
31 changes: 31 additions & 0 deletions sdl2-vga-terminal/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
cmake_minimum_required (VERSION 3.8)


############################## Main Example ##################################
add_executable (${PROJECT_NAME} "sdl2-vga-terminal.cpp")
add_dependencies(${PROJECT_NAME} vga-terminal-obj)
target_link_libraries(${PROJECT_NAME} vga-terminal-obj)


################## C example using SO/DLL with lazy loading ##################
add_executable (${PROJECT_NAME}-so "sdl2-vga-terminal-so.c")
add_dependencies(${PROJECT_NAME}-so vga-terminal)
target_link_libraries(${PROJECT_NAME}-so vga-terminal)
add_custom_command(TARGET "${PROJECT_NAME}-so" POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:vga-terminal> $<TARGET_FILE_DIR:${PROJECT_NAME}-so>
)
if(MSVC)
target_link_options(${PROJECT_NAME}-so PRIVATE
"/INCREMENTAL"
"/DELAYLOAD:$<TARGET_FILE_NAME:vga-terminal>"
"/DELAY:UNLOAD"
)
endif()


################## C example using SO/DLL run-time loading ##################
add_executable (${PROJECT_NAME}-load-so "sdl2-vga-terminal-load-so.c")
target_link_libraries(${PROJECT_NAME}-load-so PUBLIC SDL2::SDL2 SDL2::SDL2main)
add_custom_command(TARGET "${PROJECT_NAME}-load-so" POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:vga-terminal> $<TARGET_FILE_DIR:${PROJECT_NAME}-load-so>
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,46 @@
typedef void VGA_Terminal;

#ifdef WIN32

#include <Windows.h>

typedef void(__cdecl* PROCWRITEXY)(VGA_Terminal*, uint8_t, uint8_t, char* str, uint8_t, uint8_t);
typedef void(__cdecl* PROCDESTROY)(VGA_Terminal*);
typedef void(__cdecl* PROCRENDER)(VGA_Terminal*);
typedef VGA_Terminal* (__cdecl* PROCINIT)();
#endif

#ifdef linux

#include <dlfcn.h>

#endif

#if defined(DEBUG) || !defined(NDBEBUG)
#define VGA_SO_NAME "vga-terminal-libd"

#define VGA_SO_NAME "vga-terminald"

#else
#define VGA_SO_NAME "vga-terminal-lib"

#define VGA_SO_NAME "vga-terminal"

#endif

#ifdef WIN32

void win32()
{
const char* soname = VGA_SO_NAME ".dll";
const char* soname = TEXT(VGA_SO_NAME ".dll");
HINSTANCE hinstLib;
PROCINIT ProcAddInit = NULL;
PROCDESTROY ProcAddDestroy = NULL;
PROCWRITEXY ProcAddWriteXY = NULL;
PROCRENDER ProcAddRender = NULL;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;

printf("loading library: %s\n", soname);

hinstLib = LoadLibrary(TEXT("vga-terminal-libd.dll"));
hinstLib = LoadLibrary(soname);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (hinstLib != NULL)
{
ProcAddInit = (PROCINIT)GetProcAddress(hinstLib, "VGA_TERMINAL_init");
Expand All @@ -59,17 +69,21 @@ void win32()
ProcAddDestroy(term);
}
// Free the DLL module.

fFreeResult = FreeLibrary(hinstLib);
}
if (!fFreeResult) {
fprintf(stderr, "unable to unload library\n");
}
else {
printf("library unloaded.\n");
}
}

// If unable to call the DLL function, use an alternative.
if (!fRunTimeLinkSuccess) {
DWORD dw = GetLastError();

fprintf(stderr, "failed to load the DLL. Error %d\n", dw);
}

}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ int main(int argc, char* args[])
while (quit--) {
VGA_TERMINAL_render(term);
SDL_Delay(1000);
}
};

VGA_TERMINAL_destroy(term);

Expand All @@ -40,7 +40,7 @@ int main(int argc, char* args[])
#else
#define PFIX ""
#endif
int t = __FUnloadDelayLoadedDLL2("vga-terminal-lib" PFIX ".dll");
int t = __FUnloadDelayLoadedDLL2("vga-terminal" PFIX ".dll");
printf("unloaded? : %d\n", t);
getchar();
#endif // WIN32
Expand Down
Loading