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

Added Linux support #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
34 changes: 34 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
cmake_minimum_required(VERSION 3.12)
project(dod_playground)

if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Linux")
message(FATAL_ERROR "This CMake setup currently only supports Linux. For Windows and macOS please see the ready-made projects in the projects/ directory.")
endif()

set(CMAKE_C_STANDARD 99)
set(CMAKE_CXX_STANDARD 11)

find_package(X11 REQUIRED)
find_package(OpenGL REQUIRED)

add_compile_definitions(
SOKOL_GLCORE33)

include_directories(
${X11_INCLUDE_DIR})

add_executable(dod_playground
source/external/sokol.c
source/external/sokol_app.h
source/external/sokol_gfx.h
source/external/sokol_time.h
source/external/stb_easy_font.h
source/external/stb_image.h
source/application.c
source/game.cpp
source/game.h)

target_link_libraries(dod_playground
OpenGL::GL
${X11_LIBRARIES}
dl)
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ in the process.

Talk [**slides are here**](http://aras-p.info/texts/files/2018Academy%20-%20ECS-DoD.pdf) _(10MB pdf)_.

The app playground should work on Windows (uses D3D11, VS2017 project in `projects/vs2017/dod-playground.sln`) and
macOS (uses Metal, Xcode 9 project in `projects/xcode/dod-playground.xcodeproj`).
The app playground should work on Windows (uses D3D11, VS2017 project in `projects/vs2017/dod-playground.sln`),
macOS (uses Metal, Xcode 9 project in `projects/xcode/dod-playground.xcodeproj`) and Linux (uses GL, CMakeLists.txt
in root folder).

I used some excellent other libraries/resources to make life easier for me here:

Expand Down
52 changes: 51 additions & 1 deletion source/application.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,22 @@ void init(void) {
/* create shader */
sg_shader shd = sg_make_shader(&(sg_shader_desc) {
.vs.uniform_blocks[0] = {
#if defined(SOKOL_GLCORE33)
.size = sizeof(vs_params_t),
.uniforms = {
[0] = { .name = "aspect", .type = SG_UNIFORMTYPE_FLOAT }
},
#else
.size = sizeof(vs_params_t)
#endif
},
#if defined(SOKOL_GLCORE33)
.fs.images = {
[0] = { .name="tex0", .type=SG_IMAGETYPE_2D },
},
#else
.fs.images[0].type = SG_IMAGETYPE_2D,
#endif
.vs.source = vs_src,
.fs.source = fs_src,
});
Expand All @@ -78,10 +91,17 @@ void init(void) {
sg_pipeline pip = sg_make_pipeline(&(sg_pipeline_desc){
.layout = {
.buffers[0].step_func = SG_VERTEXSTEP_PER_INSTANCE,
#if defined(SOKOL_GLCORE33)
.attrs = {
[0] = { .name="posScale", .offset = 0, .format=SG_VERTEXFORMAT_FLOAT3, .buffer_index=0 }, // instance pos + scale
[1] = { .name="colorIndex", .offset = 12, .format=SG_VERTEXFORMAT_FLOAT4, .buffer_index=0 }, // instance color
},
#else
.attrs = {
[0] = { .sem_name = "POSSCALE", .format=SG_VERTEXFORMAT_FLOAT3 }, // instance pos + scale
[1] = { .sem_name = "COLORSPRITE", .format=SG_VERTEXFORMAT_FLOAT4 }, // instance color
},
#endif
},
.shader = shd,
.index_type = SG_INDEXTYPE_UINT16,
Expand Down Expand Up @@ -272,6 +292,36 @@ const char* fs_src =
" diffuse.rgb *= inp.color.rgb;\n"
" return diffuse;\n"
"}\n";
#elif defined(SOKOL_GLCORE33)
const char* vs_src =
"#version 330\n"
"uniform float aspect;\n"
"in vec3 posScale;\n"
"in vec4 colorIndex;\n"
"out vec3 color;\n"
"out vec2 uv;\n"
"void main() {\n"
" float x = gl_VertexID / 2;\n"
" float y = gl_VertexID & 1;\n"
" gl_Position.x = posScale.x + (x-0.5f) * posScale.z;\n"
" gl_Position.y = posScale.y + (y-0.5f) * posScale.z * aspect;\n"
" gl_Position.z = 0.0f;\n"
" gl_Position.w = 1.0f;\n"
" uv = vec2((x + colorIndex.w)/8, 1-y);\n"
" color = colorIndex.rgb;\n"
"}\n";
const char* fs_src =
"#version 330\n"
"uniform sampler2D tex0;\n"
"in vec3 color;\n"
"in vec2 uv;\n"
"out vec4 frag_color;\n"
"void main() {\n"
" frag_color = texture(tex0, uv);\n"
" float lum = dot(frag_color.rgb, vec3(0.333));\n"
" frag_color.rgb = mix(frag_color.rgb, vec3(lum), 0.8);\n"
" frag_color.rgb *= color.rgb;\n"
"}\n";
#else
#error Unknown graphics plaform
#error No shaders defined for specified graphics platform
#endif