Basic movement with verlet integration, window
commit
387a09bcc2
@ -0,0 +1,5 @@
|
||||
*.exe
|
||||
*.obj
|
||||
*.pdb
|
||||
*.ilk
|
||||
*.gen.h
|
@ -0,0 +1,22 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "(Windows) Launch",
|
||||
"type": "cppvsdbg",
|
||||
"request": "launch",
|
||||
"program": "${workspaceFolder}/flight.exe",
|
||||
"args": [],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "${workspaceFolder}",
|
||||
"symbolSearchPath": "${workspaceFolder}",
|
||||
"preLaunchTask": "build",
|
||||
"environment": [],
|
||||
"console": "externalTerminal"
|
||||
}
|
||||
|
||||
]
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"sokol_app.h": "c",
|
||||
"triangle.gen.h": "c",
|
||||
"*.rh": "c",
|
||||
"charconv": "c",
|
||||
"chrono": "c",
|
||||
"format": "c",
|
||||
"forward_list": "c",
|
||||
"iomanip": "c",
|
||||
"ios": "c",
|
||||
"iosfwd": "c",
|
||||
"istream": "c",
|
||||
"list": "c",
|
||||
"optional": "c",
|
||||
"ostream": "c",
|
||||
"sstream": "c",
|
||||
"stop_token": "c",
|
||||
"string": "c",
|
||||
"thread": "c",
|
||||
"xhash": "c",
|
||||
"xiosbase": "c",
|
||||
"xlocale": "c",
|
||||
"xlocbuf": "c",
|
||||
"xstring": "c",
|
||||
"algorithm": "c"
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
{
|
||||
// See https://go.microsoft.com/fwlink/?LinkId=733558
|
||||
// for the documentation about the tasks.json format
|
||||
"version": "2.0.0",
|
||||
"tasks": [
|
||||
{
|
||||
"type": "shell",
|
||||
"label": "build",
|
||||
"windows": {
|
||||
"command": "call build_debug.bat",
|
||||
},
|
||||
"presentation": {
|
||||
"echo": true,
|
||||
"reveal": "always",
|
||||
"focus": false,
|
||||
"panel": "shared",
|
||||
"showReuseMessage": false,
|
||||
"clear": true
|
||||
}
|
||||
},
|
||||
]
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
@echo off
|
||||
|
||||
@REM what all the flags mean: https://learn.microsoft.com/en-us/cpp/build/reference/compiler-options-listed-by-category?view=msvc-170
|
||||
|
||||
WHERE sokol-shdc.exe
|
||||
IF %ERRORLEVEL% NEQ 0 ECHO ERROR download sokol-shdc from https://github.com/floooh/sokol-tools-bin/blob/master/bin/win32/sokol-shdc.exe and put it in this folder
|
||||
|
||||
@REM example of how to compile shaders: sokol-shdc.exe --input triangle.glsl --output triangle.gen.h --slang glsl330:hlsl5:metal_macos
|
||||
|
||||
cl /MP /Zi /Fd"flight.pdb" /I"thirdparty" /Fe"flight" main.c
|
@ -0,0 +1,237 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// Take flight
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#define SOKOL_IMPL
|
||||
#define SOKOL_D3D11
|
||||
#include "sokol_gfx.h"
|
||||
#include "sokol_gp.h"
|
||||
#include "sokol_app.h"
|
||||
#include "sokol_glue.h"
|
||||
|
||||
#define MAX_BOXES 32
|
||||
#define BOX_SIZE 0.5f
|
||||
#define TIMESTEP 1.0f / 60.0f
|
||||
struct Body
|
||||
{
|
||||
sgp_point position;
|
||||
sgp_point old_position;
|
||||
sgp_vec2 acceleration;
|
||||
};
|
||||
struct GameState
|
||||
{
|
||||
struct Player
|
||||
{
|
||||
struct Body body;
|
||||
} player;
|
||||
|
||||
int num_boxes;
|
||||
struct Box
|
||||
{
|
||||
struct Body body;
|
||||
} boxes[MAX_BOXES];
|
||||
};
|
||||
|
||||
struct GameState gs = {0};
|
||||
bool mouse_down = false;
|
||||
bool keydown[SAPP_KEYCODE_MENU] = {0};
|
||||
float funval = 0.0f; // easy to play with value controlled by left mouse button when held down @BeforeShip remove on release builds
|
||||
|
||||
void init(void)
|
||||
{
|
||||
// @BeforeShip make all fprintf into logging to file, warning dialog boxes on failure instead of exit(-1), replace the macros in sokol with this as well, like assert
|
||||
|
||||
sg_desc sgdesc = {.context = sapp_sgcontext()};
|
||||
sg_setup(&sgdesc);
|
||||
if (!sg_isvalid())
|
||||
{
|
||||
fprintf(stderr, "Failed to create Sokol GFX context!\n");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
gs.boxes[0] = (struct Box){
|
||||
.body = (struct Body){
|
||||
.position = (sgp_point){.x = 0.75f, .y = 0.0}},
|
||||
};
|
||||
gs.boxes[0].body.old_position = gs.boxes[0].body.position;
|
||||
gs.boxes[1] = (struct Box){
|
||||
.body = (struct Body){
|
||||
.position = (sgp_point){.x = 0.75f, .y = 0.5f}},
|
||||
};
|
||||
gs.boxes[1].body.old_position = gs.boxes[1].body.position;
|
||||
gs.num_boxes = 2;
|
||||
|
||||
sgp_desc sgpdesc = {0};
|
||||
sgp_setup(&sgpdesc);
|
||||
if (!sgp_is_valid())
|
||||
{
|
||||
fprintf(stderr, "Failed to create Sokol GP context: %s\n", sgp_get_error_message(sgp_get_last_error()));
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
sgp_vec2 v2add(sgp_vec2 a, sgp_vec2 b)
|
||||
{
|
||||
return (sgp_vec2){
|
||||
.x = a.x + b.x,
|
||||
.y = a.y + b.y,
|
||||
};
|
||||
}
|
||||
|
||||
sgp_vec2 v2scale(sgp_vec2 a, float f)
|
||||
{
|
||||
return (sgp_vec2){
|
||||
.x = a.x * f,
|
||||
.y = a.y * f,
|
||||
};
|
||||
}
|
||||
|
||||
sgp_vec2 v2sub(sgp_vec2 a, sgp_vec2 b)
|
||||
{
|
||||
return (sgp_vec2){
|
||||
.x = a.x - b.x,
|
||||
.y = a.y - b.y,
|
||||
};
|
||||
}
|
||||
|
||||
void process_body(struct Body *body)
|
||||
{
|
||||
sgp_vec2 current = body->position;
|
||||
body->position = v2add(body->position, v2sub(current, body->old_position));
|
||||
body->position = v2add(body->position, v2scale(body->acceleration, TIMESTEP*TIMESTEP));
|
||||
body->old_position = current;
|
||||
}
|
||||
|
||||
void frame(void)
|
||||
{
|
||||
int width = sapp_width(), height = sapp_height();
|
||||
float ratio = width / (float)height;
|
||||
float time = sapp_frame_count() * sapp_frame_duration();
|
||||
|
||||
// gameplay
|
||||
{
|
||||
sgp_vec2 input = (sgp_vec2){
|
||||
.x = (float)keydown[SAPP_KEYCODE_D] - (float)keydown[SAPP_KEYCODE_A],
|
||||
.y = (float)keydown[SAPP_KEYCODE_S] - (float)keydown[SAPP_KEYCODE_W],
|
||||
};
|
||||
gs.player.body.acceleration = v2scale(input, 5.0f);
|
||||
|
||||
process_body(&gs.player.body);
|
||||
for(int i = 0; i < gs.num_boxes; i++)
|
||||
{
|
||||
process_body(&gs.boxes[i].body);
|
||||
}
|
||||
}
|
||||
|
||||
// drawing
|
||||
{
|
||||
sgp_begin(width, height);
|
||||
sgp_viewport(0, 0, width, height);
|
||||
// sgp_project(-ratio, ratio, 1.0f, -1.0f);
|
||||
sgp_project(0.0f, width, 0.0f, height);
|
||||
|
||||
// Draw background color
|
||||
sgp_set_color(0.1f, 0.1f, 0.1f, 1.0f);
|
||||
sgp_clear();
|
||||
|
||||
// Drawing in world space now
|
||||
// sgp_translate(gs.player.body.position.x, gs.player.body.position.y);
|
||||
sgp_translate(width / 2, height / 2);
|
||||
sgp_scale_at(300.0f + funval, 300.0f + funval, 0.0f, 0.0f);
|
||||
|
||||
sgp_set_color(1.0f, 1.0f, 1.0f, 1.0f);
|
||||
sgp_draw_filled_rect(100.0f, 100.0f, 400.0f, 400.0f);
|
||||
|
||||
// stars
|
||||
const int num = 50;
|
||||
for (int x = -num; x < num; x++)
|
||||
{
|
||||
for (int y = -num; y < num; y++)
|
||||
{
|
||||
sgp_draw_point((float)x * 0.1f, (float)y * 0.1f);
|
||||
}
|
||||
}
|
||||
|
||||
float halfbox = BOX_SIZE / 2.0f;
|
||||
|
||||
// player
|
||||
{
|
||||
sgp_set_color(1.0f, 1.0f, 1.0f, 1.0f);
|
||||
sgp_draw_filled_rect(gs.player.body.position.x - halfbox, gs.player.body.position.y - halfbox, BOX_SIZE, BOX_SIZE);
|
||||
}
|
||||
|
||||
// boxes
|
||||
{
|
||||
sgp_set_color(0.5f, 0.5f, 0.5f, 1.0f);
|
||||
for (int i = 0; i < gs.num_boxes; i++)
|
||||
{
|
||||
sgp_draw_filled_rect(gs.boxes[i].body.position.x - halfbox, gs.boxes[i].body.position.y - halfbox, BOX_SIZE, BOX_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
// sgp_draw_line(5.0f, 5.0f, 5.0f, 10.0f);
|
||||
// sgp_draw_line()
|
||||
// sgp_rotate_at(time, 0.0f, 0.0f);
|
||||
|
||||
// Begin a render pass.
|
||||
sg_pass_action pass_action = {0};
|
||||
sg_begin_default_pass(&pass_action, width, height);
|
||||
sgp_flush();
|
||||
sgp_end();
|
||||
sg_end_pass();
|
||||
sg_commit();
|
||||
}
|
||||
}
|
||||
|
||||
void cleanup(void)
|
||||
{
|
||||
sgp_shutdown();
|
||||
sg_shutdown();
|
||||
}
|
||||
|
||||
void event(const sapp_event *e)
|
||||
{
|
||||
switch (e->type)
|
||||
{
|
||||
case SAPP_EVENTTYPE_KEY_DOWN:
|
||||
keydown[e->key_code] = true;
|
||||
break;
|
||||
case SAPP_EVENTTYPE_KEY_UP:
|
||||
keydown[e->key_code] = false;
|
||||
break;
|
||||
case SAPP_EVENTTYPE_MOUSE_DOWN:
|
||||
if (e->mouse_button == SAPP_MOUSEBUTTON_LEFT)
|
||||
mouse_down = true;
|
||||
break;
|
||||
case SAPP_EVENTTYPE_MOUSE_UP:
|
||||
if (e->mouse_button == SAPP_MOUSEBUTTON_LEFT)
|
||||
mouse_down = false;
|
||||
break;
|
||||
case SAPP_EVENTTYPE_MOUSE_MOVE:
|
||||
if (mouse_down)
|
||||
{
|
||||
funval += e->mouse_dx;
|
||||
printf("Funval %f\n", funval);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
sapp_desc sokol_main(int argc, char *argv[])
|
||||
{
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
return (sapp_desc){
|
||||
.init_cb = init,
|
||||
.frame_cb = frame,
|
||||
.cleanup_cb = cleanup,
|
||||
.width = 640,
|
||||
.height = 480,
|
||||
.gl_force_gles2 = true,
|
||||
.window_title = "Flight",
|
||||
.icon.sokol_default = true,
|
||||
.event_cb = event,
|
||||
.win32_console_attach = true,
|
||||
.sample_count = 4, // anti aliasing
|
||||
};
|
||||
}
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,137 @@
|
||||
#if defined(SOKOL_IMPL) && !defined(SOKOL_GLUE_IMPL)
|
||||
#define SOKOL_GLUE_IMPL
|
||||
#endif
|
||||
#ifndef SOKOL_GLUE_INCLUDED
|
||||
/*
|
||||
sokol_glue.h -- glue helper functions for sokol headers
|
||||
|
||||
Project URL: https://github.com/floooh/sokol
|
||||
|
||||
Do this:
|
||||
#define SOKOL_IMPL or
|
||||
#define SOKOL_GLUE_IMPL
|
||||
before you include this file in *one* C or C++ file to create the
|
||||
implementation.
|
||||
|
||||
...optionally provide the following macros to override defaults:
|
||||
|
||||
SOKOL_ASSERT(c) - your own assert macro (default: assert(c))
|
||||
SOKOL_GLUE_API_DECL - public function declaration prefix (default: extern)
|
||||
SOKOL_API_DECL - same as SOKOL_GLUE_API_DECL
|
||||
SOKOL_API_IMPL - public function implementation prefix (default: -)
|
||||
|
||||
If sokol_glue.h is compiled as a DLL, define the following before
|
||||
including the declaration or implementation:
|
||||
|
||||
SOKOL_DLL
|
||||
|
||||
On Windows, SOKOL_DLL will define SOKOL_GLUE_API_DECL as __declspec(dllexport)
|
||||
or __declspec(dllimport) as needed.
|
||||
|
||||
OVERVIEW
|
||||
========
|
||||
The sokol core headers should not depend on each other, but sometimes
|
||||
it's useful to have a set of helper functions as "glue" between
|
||||
two or more sokol headers.
|
||||
|
||||
This is what sokol_glue.h is for. Simply include the header after other
|
||||
sokol headers (both for the implementation and declaration), and
|
||||
depending on what headers have been included before, sokol_glue.h
|
||||
will make available "glue functions".
|
||||
|
||||
PROVIDED FUNCTIONS
|
||||
==================
|
||||
|
||||
- if sokol_app.h and sokol_gfx.h is included:
|
||||
|
||||
sg_context_desc sapp_sgcontext(void):
|
||||
|
||||
Returns an initialized sg_context_desc function initialized
|
||||
by calling sokol_app.h functions.
|
||||
|
||||
LICENSE
|
||||
=======
|
||||
zlib/libpng license
|
||||
|
||||
Copyright (c) 2018 Andre Weissflog
|
||||
|
||||
This software is provided 'as-is', without any express or implied warranty.
|
||||
In no event will the authors be held liable for any damages arising from the
|
||||
use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software in a
|
||||
product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
|
||||
2. Altered source versions must be plainly marked as such, and must not
|
||||
be misrepresented as being the original software.
|
||||
|
||||
3. This notice may not be removed or altered from any source
|
||||
distribution.
|
||||
*/
|
||||
#define SOKOL_GLUE_INCLUDED
|
||||
|
||||
#if defined(SOKOL_API_DECL) && !defined(SOKOL_GLUE_API_DECL)
|
||||
#define SOKOL_GLUE_API_DECL SOKOL_API_DECL
|
||||
#endif
|
||||
#ifndef SOKOL_GLUE_API_DECL
|
||||
#if defined(_WIN32) && defined(SOKOL_DLL) && defined(SOKOL_GLUE_IMPL)
|
||||
#define SOKOL_GLUE_API_DECL __declspec(dllexport)
|
||||
#elif defined(_WIN32) && defined(SOKOL_DLL)
|
||||
#define SOKOL_GLUE_API_DECL __declspec(dllimport)
|
||||
#else
|
||||
#define SOKOL_GLUE_API_DECL extern
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if defined(SOKOL_GFX_INCLUDED) && defined(SOKOL_APP_INCLUDED)
|
||||
SOKOL_GLUE_API_DECL sg_context_desc sapp_sgcontext(void);
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
#endif /* SOKOL_GLUE_INCLUDED */
|
||||
|
||||
/*-- IMPLEMENTATION ----------------------------------------------------------*/
|
||||
#ifdef SOKOL_GLUE_IMPL
|
||||
#define SOKOL_GLUE_IMPL_INCLUDED (1)
|
||||
#include <string.h> /* memset */
|
||||
|
||||
#ifndef SOKOL_API_IMPL
|
||||
#define SOKOL_API_IMPL
|
||||
#endif
|
||||
|
||||
#if defined(SOKOL_GFX_INCLUDED) && defined(SOKOL_APP_INCLUDED)
|
||||
SOKOL_API_IMPL sg_context_desc sapp_sgcontext(void) {
|
||||
sg_context_desc desc;
|
||||
memset(&desc, 0, sizeof(desc));
|
||||
desc.color_format = (sg_pixel_format) sapp_color_format();
|
||||
desc.depth_format = (sg_pixel_format) sapp_depth_format();
|
||||
desc.sample_count = sapp_sample_count();
|
||||
desc.gl.force_gles2 = sapp_gles2();
|
||||
desc.metal.device = sapp_metal_get_device();
|
||||
desc.metal.renderpass_descriptor_cb = sapp_metal_get_renderpass_descriptor;
|
||||
desc.metal.drawable_cb = sapp_metal_get_drawable;
|
||||
desc.d3d11.device = sapp_d3d11_get_device();
|
||||
desc.d3d11.device_context = sapp_d3d11_get_device_context();
|
||||
desc.d3d11.render_target_view_cb = sapp_d3d11_get_render_target_view;
|
||||
desc.d3d11.depth_stencil_view_cb = sapp_d3d11_get_depth_stencil_view;
|
||||
desc.wgpu.device = sapp_wgpu_get_device();
|
||||
desc.wgpu.render_view_cb = sapp_wgpu_get_render_view;
|
||||
desc.wgpu.resolve_view_cb = sapp_wgpu_get_resolve_view;
|
||||
desc.wgpu.depth_stencil_view_cb = sapp_wgpu_get_depth_stencil_view;
|
||||
return desc;
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* SOKOL_GLUE_IMPL */
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,14 @@
|
||||
#NoEnv
|
||||
#SingleInstance, Force
|
||||
SendMode, Input
|
||||
SetBatchLines, -1
|
||||
SetWorkingDir, %A_ScriptDir%
|
||||
|
||||
|
||||
^+b::
|
||||
WinKill, Flight
|
||||
WinActivate, flightbuild
|
||||
If WinActive("flightbuild")
|
||||
{
|
||||
Send, build_debug.bat && flight.exe{Enter}
|
||||
}
|
Loading…
Reference in New Issue