diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..851ba92 --- /dev/null +++ b/CMakeLists.txt @@ -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) diff --git a/README.md b/README.md index b4a42ae..78891d8 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/source/application.c b/source/application.c index c1378b7..f12148d 100644 --- a/source/application.c +++ b/source/application.c @@ -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, }); @@ -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, @@ -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