diff options
25 files changed, 466 insertions, 388 deletions
diff --git a/CHANGELOG.txt b/CHANGELOG.txt index c866da4..e51b7ab 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,3 +1,15 @@ +# 0.8.0 + +* Make code more modular; +* Don't use global variables; +* Add more launch options; +* Fix memory leak; +* Add abort routine; +* Add copyright notices to source files; +* Add patch version to version number; +* Enable more warnings; +* Draw simulation in window; + # 0.7 * Make window fullscreen; diff --git a/bowshock/GNUmakefile b/bowshock/GNUmakefile index ce22f4a..cc6cc2c 100644 --- a/bowshock/GNUmakefile +++ b/bowshock/GNUmakefile @@ -1,18 +1,26 @@ +# Copyright 2022-2023 Gabriel Jensen. + LD = $(CC) CFLAGS := \ - -Iinclude \ - -Wall \ - -Wextra \ - -Wpedantic \ - -Wno-attributes \ - -Wno-gnu-folding-constant \ - -Wno-gnu-empty-initializer \ - -Wno-gnu-zero-variadic-macro-arguments \ - -g \ - -pipe \ + -Iinclude \ + -Wall \ + -Wextra \ + -Wmissing-prototypes \ + -Wpedantic \ + -Wno-attributes \ + -g \ + -pipe \ -std=gnu2x +ifeq "$(CC)" "clang" +CFLAGS := \ + $(CFLAGS) \ + -Wno-gnu-folding-constant \ + -Wno-gnu-empty-initializer \ + -Wno-gnu-zero-variadic-macro-arguments +endif + ifeq "$(c2xcompat)" "true" CFLAGS := \ $(CFLAGS) \ @@ -39,20 +47,16 @@ CFLAGS := \ -Ofast endif -LDLIBS := \ - -lGL \ - -lflux \ - -lglfw \ - -lm \ - -lwebp \ - -lzap - OBJS := \ + source/bs/abrt.o \ source/bs/gendat.o \ + source/bs/getquot.o \ + source/bs/getsavpth.o \ + source/bs/help.o \ source/bs/init.o \ - source/bs/initdat.o \ source/bs/loop.o \ source/bs/quit.o \ + source/gfx/drw.o \ source/gfx/initgfx.o \ source/info/objtypstr.o \ source/info/shipmass.o \ @@ -63,6 +67,13 @@ OBJS := \ source/sav/cont.o \ source/sav/sav.o +LDLIBS := \ + -lGL \ + -lflux \ + -lglfw \ + -lm \ + -lzap + BIN := bowshock.elf HDRS := \ diff --git a/bowshock/include/bow/bs.h b/bowshock/include/bow/bs.h index 46cc4ec..72c4d3e 100644 --- a/bowshock/include/bow/bs.h +++ b/bowshock/include/bow/bs.h @@ -1,3 +1,5 @@ +// Copyright 2022-2023 Gabriel Jensen. + #pragma once #define constexpr static const @@ -12,8 +14,6 @@ //#error ISO/IEC 60559:2020 is required! //#endif -#pragma STDC FP_CONTRACT ON - #include <limits.h> #include <signal.h> #include <stdio.h> @@ -28,28 +28,23 @@ static_assert(sizeof (zap_i04) == 0x8u); static_assert(sizeof (float) == 0x4u); static_assert(sizeof (double) == 0x8u); -#if !defined(bow_sym) -#error symbol not defined -#endif - #define bow_rawlog(msg,...) fprintf(stderr,msg __VA_OPT__(,) __VA_ARGS__) -#define bow_log(msg,...) bow_rawlog("\x1B[1m%s\x1B[0m: " msg "\n",bow_sym __VA_OPT__(,) __VA_ARGS__) +#define bow_log(msg,...) bow_rawlog("\x1B[1m%s\x1B[0m: " msg "\n",__func__ __VA_OPT__(,) __VA_ARGS__) #define bow_logerr(msg,...) bow_log("\x1B[38;5;197m[ERROR]\x1B[0m " msg __VA_OPT__(,) __VA_ARGS__) #if bow_dbg #define bow_dbglog(msg,...) bow_log(msg __VA_OPT__ (,) __VA_ARGS__) - -#define bow_logxyz(xyz) bow_rawlog("x: %f\ny: %f\nz: %f\n\n",xyz.x,xyz.y,xyz.z) #else #define bow_dbglog(msg,...) ((void)0x0u) - -#define bow_logxyz(xyz) ((void)0x0u) #endif +#define bow_logxyz(xyz) bow_dbglog("%.03f %.03f %.03f",(xyz).x,(xyz).y,(xyz).z) + constexpr zap_i04 bow_vermaj = 0x0u; -constexpr zap_i04 bow_vermin = 0x7u; +constexpr zap_i04 bow_vermin = 0x8u; +constexpr zap_i04 bow_verpat = 0x0u; #define bow_cmdrnmlen ((zap_sz)0xEu) @@ -58,96 +53,28 @@ typedef enum { bow_stat_err = 0x1u, } bow_stat; -typedef enum { - bow_objtyp_can, // canister - bow_objtyp_play, // player - bow_objtyp_sat, // satellite (moon) - bow_objtyp_ship, // ship - bow_objtyp_star, // star - bow_objtyp_station, // station - bow_objtyp_wrld, // world (planet) -} bow_objtyp; - -typedef enum { - bow_wrld_amm, // ammonium world - bow_wrld_gas, // gas giant - bow_wrld_ice, // icy world - bow_wrld_rck, // rocky world - bow_wrld_lav, // lava world - bow_wrld_wat, // water world -} bow_wrld; - -typedef enum { - bow_ship_aq, // aquila - bow_ship_cas, // cassiopeia - bow_ship_cent, // centaurus - bow_ship_eri, // eridanus - bow_ship_lyra, // lyra - bow_ship_tau, // taurus - bow_ship_ursa, // ursa -} bow_ship; - -typedef enum { - bow_star_a, // main sequence - bow_star_b, // main sequence - bow_star_c, // carbon - bow_star_f, // main sequence - bow_star_g, // main sequence - bow_star_k, // main sequence - bow_star_l, // brown dwarf - bow_star_m, // main sequence - bow_star_n, // neutron star - bow_star_o, // main sequence - bow_star_s, // carbon - bow_star_t, // brown dwarf - bow_star_w, // worm hole - bow_star_x, // black hole - bow_star_y, // brown dwarf - bow_star_z, // white hole -} bow_star; - -typedef enum { - bow_station_cor, // coriolis -} bow_station; - typedef struct { double x; double y; double z; } bow_xyz; -typedef struct bow_priv_obj bow_obj; - -struct bow_priv_obj { - bow_objtyp typ; - bow_xyz pos; - bow_xyz rot; // radians - bow_xyz posvel; - bow_xyz rotvel; - double mass; - union { - bow_wrld wrldtyp; - bow_ship shiptyp; - bow_star startyp; - bow_station stationtyp; - }; - bow_obj * next; -}; +typedef struct bow_impl_obj bow_obj; -typedef struct { - bow_obj * objs; -} bow_objroot; +typedef struct bow_impl_objroot bow_objroot; -typedef struct { - char nm[bow_cmdrnmlen+0x1u]; - zap_i04 tm; - zap_i04 sysid; - bow_obj ship; -} bow_playdat; +typedef struct bow_impl_playdat bow_playdat; + +typedef struct bow_impl_gfxdat bow_gfxdat; extern sig_atomic_t volatile bow_gotintr; -void bow_gendat( bow_playdat * playdat); -void bow_initdat(bow_playdat * playdat,char const * * savpth); +char const * bow_getsavpth(void); +bool bow_getquot( char const * * quot,char const * * src,zap_i8 id); + +bow_stat bow_loop(bow_gfxdat * gfxdat,bow_playdat * playdat); + +[[noreturn]] void bow_help(char const * prognm); -[[noreturn]] void bow_quit(bow_stat stat); +[[noreturn]] void bow_abrt(void); +[[noreturn]] void bow_quit(bow_gfxdat * gfxdat,bow_stat stat); diff --git a/bowshock/include/bow/gfx.h b/bowshock/include/bow/gfx.h index d6b858f..45e6761 100644 --- a/bowshock/include/bow/gfx.h +++ b/bowshock/include/bow/gfx.h @@ -1,13 +1,16 @@ +// Copyright 2022-2023 Gabriel Jensen. + #pragma once #include <bow/bs.h> #include <GLFW/glfw3.h> -extern struct { +struct bow_impl_gfxdat { GLFWwindow * win; int frmbufw; int frmbufh; -} bow_gfxdat; +}; -void bow_initgfx(void); +void bow_drw( bow_gfxdat * gfxdat,bow_objroot * sys,bow_objroot * objs); +void bow_initgfx(bow_gfxdat * dat); diff --git a/bowshock/include/bow/info.h b/bowshock/include/bow/info.h index 09da580..d8ff852 100644 --- a/bowshock/include/bow/info.h +++ b/bowshock/include/bow/info.h @@ -1,6 +1,8 @@ +// Copyright 2022-2023 Gabriel Jensen. + #pragma once -#include <bow/bs.h> +#include <bow/lgc.h> char const * bow_objtypstr(bow_objtyp typ); diff --git a/bowshock/include/bow/lgc.h b/bowshock/include/bow/lgc.h index bea97e8..585ea8f 100644 --- a/bowshock/include/bow/lgc.h +++ b/bowshock/include/bow/lgc.h @@ -1,14 +1,93 @@ +// Copyright 2022-2023 Gabriel Jensen. + #pragma once #include <bow/bs.h> #include <zap/bs.h> -constexpr double bow_distmod = 0x1p0; // distance modifier -constexpr double bow_massmod = 0x1p0; // mass modifier -constexpr double bow_tmmod = 0x1p0; // time modifier +typedef enum { + bow_objtyp_can, // canister + bow_objtyp_play, // player + bow_objtyp_sat, // satellite (moon) + bow_objtyp_ship, // ship + bow_objtyp_star, // star + bow_objtyp_station, // station + bow_objtyp_wrld, // world (planet) +} bow_objtyp; + +typedef enum { + bow_wrld_amm, // ammonium world + bow_wrld_gas, // gas giant + bow_wrld_ice, // icy world + bow_wrld_rck, // rocky world + bow_wrld_lav, // lava world + bow_wrld_wat, // water world +} bow_wrld; + +typedef enum { + bow_ship_aq, // aquila + bow_ship_cas, // cassiopeia + bow_ship_cent, // centaurus + bow_ship_eri, // eridanus + bow_ship_lyra, // lyra + bow_ship_tau, // taurus + bow_ship_ursa, // ursa +} bow_ship; + +typedef enum { + bow_star_a, // main sequence + bow_star_b, // main sequence + bow_star_c, // carbon + bow_star_f, // main sequence + bow_star_g, // main sequence + bow_star_k, // main sequence + bow_star_l, // brown dwarf + bow_star_m, // main sequence + bow_star_n, // neutron star + bow_star_o, // main sequence + bow_star_s, // carbon + bow_star_t, // brown dwarf + bow_star_w, // worm hole + bow_star_x, // black hole + bow_star_y, // brown dwarf + bow_star_z, // white hole +} bow_star; + +typedef enum { + bow_station_cor, // coriolis +} bow_station; + +struct bow_impl_obj { + bow_objtyp typ; + bow_xyz pos; + bow_xyz rot; // radians + bow_xyz posvel; + bow_xyz rotvel; + double mass; + union { + bow_wrld wrldtyp; + bow_ship shiptyp; + bow_star startyp; + bow_station stationtyp; + }; + bow_obj * next; +}; + +struct bow_impl_objroot { + bow_obj * objs; +}; + +struct bow_impl_playdat { + char nm[bow_cmdrnmlen+0x1u]; + zap_i04 tm; + zap_i04 sysid; + bow_obj ship; +}; + +constexpr double bow_tmmod = 0x1p-14; // time modifier -constexpr double bow_gravconst = 0x1.2589EFFFp-34 * (bow_distmod*bow_distmod*bow_distmod)/(bow_massmod*bow_tmmod*bow_tmmod); // gravitational constant (s^2*m*t^2) +constexpr double bow_gravconst = 0x1.2589EFFFp-34/(bow_tmmod*bow_tmmod); // gravitational constant (s^2*m*t^2) bow_obj * bow_addobj( bow_objroot * root,bow_obj const * obj); void bow_freeobjs(bow_objroot const * root); diff --git a/bowshock/include/bow/sav.h b/bowshock/include/bow/sav.h index defa9ad..1a0ff11 100644 --- a/bowshock/include/bow/sav.h +++ b/bowshock/include/bow/sav.h @@ -1,3 +1,5 @@ +// Copyright 2022-2023 Gabriel Jensen. + #pragma once #include <bow/bs.h> @@ -51,5 +53,7 @@ typedef struct { double shiprotvelz; } bow_savdat; -void bow_cont(char const * fil,bow_playdat * dat); -void bow_sav( char const * fil,bow_playdat const * dat); +void bow_cont( char const * fil, bow_playdat * dat,bool skipld); +void bow_gendat(bow_playdat * playdat); + +void bow_sav(char const * fil,bow_playdat const * dat); diff --git a/bowshock/source/bs/abrt.c b/bowshock/source/bs/abrt.c new file mode 100644 index 0000000..768e403 --- /dev/null +++ b/bowshock/source/bs/abrt.c @@ -0,0 +1,10 @@ +// Copyright 2022-2023 Gabriel Jensen. + +#include <bow/bs.h> + +#include <stdlib.h> + +void bow_abrt(void) { + bow_log("aborting"); + abort(); +} diff --git a/bowshock/source/bs/gendat.c b/bowshock/source/bs/gendat.c index 47c131f..6d81876 100644 --- a/bowshock/source/bs/gendat.c +++ b/bowshock/source/bs/gendat.c @@ -1,13 +1,14 @@ -#define bow_sym "gendat" +// Copyright 2022-2023 Gabriel Jensen. #include <bow/info.h> +#include <bow/sav.h> #include <zap/mem.h> #include <string.h> void bow_gendat(bow_playdat * const playdatptr) { - bow_log("generating data"); + bow_log("generating player data"); bow_playdat playdat; zap_cp(&playdat,playdatptr,sizeof (playdat)); playdat.ship.typ = bow_objtyp_ship; diff --git a/bowshock/source/bs/getquot.c b/bowshock/source/bs/getquot.c new file mode 100644 index 0000000..2f5bbe3 --- /dev/null +++ b/bowshock/source/bs/getquot.c @@ -0,0 +1,143 @@ +// Copyright 2022-2023 Gabriel Jensen. + +#include <bow/bs.h> + +bool bow_getquot(char const * * const quot,char const * * src,zap_i8 const id) { + switch (id) { + default: + return true; + case 0x0u: + *quot = "You gotta be heaven to see heaven."; + *src = "Jim Carrey"; + break; + case 0x1u: + *quot = "Though it's the end of the world, don't blame yourself, now."; + *src = "Porter Robinson"; + break; + case 0x2u: + *quot = "The future will be better tomorrow."; + *src = "Dan Quayle"; + break; + case 0x3u: + *quot = "Sir, an equation has no meaning for me unless it expresses a thought of god."; + *src = "Srinivasa Ramanujan Aiyangar"; + break; + case 0x4u: + *quot = "Amīcus Platō amīcus Aristotelēs magis amīca vēritās."; + *src = "Isaac Newton"; + break; + case 0x5u: + *quot = "I have studied these things \u2013 you have not."; + *src = "Isaac Newton"; + break; + case 0x6u: + *quot = "La construction d'une machine propre à exprimer tous les sons de nos paroles, avec toutes les articulations, serait sans doute une décourverte bien importante.\n... La chose ne me paraît pas impossible."; + *src = "Leonhard Euler"; + break; + case 0x7u: + *quot = "In mathematics, you don't understand things, you just get used to them."; + *src = "John von Neumann"; + break; + case 0x8u: + *quot = "Being a language, mathematics may be used not only to inform, but also \u2013 among other things \u2013 to seduce."; + *src = "Benoît B. Mandelbrot"; + break; + case 0x9u: + *quot = "The real question is not whether machines think, but whether men do."; + *src = "Burrhus Frederic Skinner"; + break; + case 0xAu: + *quot = "Those who are not shocked when they first come across quantum theory cannot possibly have understood it."; + *src = "Niels Henrik David Bohr"; + break; + case 0xBu: + *quot = "Every sentence I utter must be understood not as an affirmation, but as a question."; + *src = "Niels Henrik David Bohr"; + break; + case 0xCu: + *quot = "We will now discuss in a little more detail the struggle for existence."; + *src = "Charles Robert Darwin"; + break; + case 0xDu: + *quot = "Nam et ipsa scientia potestas est."; + *src = "Francis Bacon"; + break; + case 0xEu: + *quot = "We don't know a millionth of one percent about anything."; + *src = "Thomas Alva Edison"; + break; + case 0xFu: + *quot = "My goal is simple. It is a complete understanding of the universe, why it is as it is, and why it exists at all."; + *src = "Stephen William Hawking"; + break; + case 0x10u: + *quot = "Equipped with his five senses, man explores the universe around him and calls the adventure Science."; + *src = "Edwin Powell Hubble"; + break; + case 0x11u: + *quot = "I can say this: I believe that the human mind, or even the mind of a cat, is more interesting in its complexity than an entire galaxy if it is devoid of life."; + *src = "Martin Gardner"; + break; + case 0x12u: + *quot = "I'm always right. This time I'm just more right than usual."; + *src = "Linus Benedict Torvalds"; + break; + case 0x13u: + *quot = "I'm an instant star. Just add water and stir."; + *src = "David Robert Jones"; + break; + case 0x14u: + *quot = "Don't waste the Earth \u2013 it is our jewel!"; + *src = "Buzz Eugene Aldrin"; + break; + case 0x15u: + *quot = "I think we're going to the moon because it's in the nature of the human being to face challenges."; + *src = "Neil Alden Armstrong"; + break; + case 0x16u: + *quot = "A hacker is someone who enjoys playful cleverness \u2013 not necessarily with computers."; + *src = "Richard Matthew Stallman"; + break; + case 0x17u: + *quot = "So Einstein was wrong when he said \"God noes not play dice.\". Consideration of black holes suggests, not only that God does play dice, but that he sometimes confuses us by throwing them where they can't be seen."; + *src = "Stephen William Hawking"; + break; + case 0x18u: + *quot = "I'm a blackstar."; + *src = "David Robert Jones"; + break; + case 0x19u: + *quot = "Sooner or later, we must expand life beyond our little blue mud ball \u2013 or go extinct."; + *src = "Elon Reeve Musk"; + break; + case 0x1Au: + *quot = "I would like to die on Mars \u2013 just not on impact."; + *src = "Elon Reeve Musk"; + break; + case 0x1Bu: + *quot = "The web does not just connect computers; it connects people."; + *src = "Timothy John Berners-Lee"; + break; + case 0x1Cu: + *quot = "Can digital computers think?"; + *src = "Alan Mathison Turing"; + break; + case 0x1Du: + *quot = "That's one small step for a man, one giant leap for mankind."; + *src = "Neil Alden Armstrong"; + break; + case 0x1Eu: + *quot = "If you think it's simple, then you have misunderstood the problem."; + *src = "Bjarne Stroustrup"; + break; + case 0x1Fu: + *quot = "Controlling complexity is the essence of computer programming."; + *src = "Brian Wilson Kerningham"; + break; + case 0x20u: + *quot = "I have always wished for my computer to be as easy to use as my telephone. My wish has come true because I can no longer figure out how to use my telephone."; + *src = "Bjarne Stroustrup"; + break; + } + return false; +} diff --git a/bowshock/source/bs/initdat.c b/bowshock/source/bs/getsavpth.c index 01c3cab..173d904 100644 --- a/bowshock/source/bs/initdat.c +++ b/bowshock/source/bs/getsavpth.c @@ -1,30 +1,30 @@ -#define bow_sym "initdat" +// Copyright 2022-2023 Gabriel Jensen. -#include <bow/sav.h> +#include <bow/bs.h> #include <flux/stats.h> #include <stdlib.h> #include <zap/mem.h> #include <zap/str.h> -#define bow_setstr(ptr,len,str) ((void)(ptr = str,len = sizeof (str))) +#define bow_setstrlen(ptr,len,str) ((void)(ptr = str,len = sizeof (str))) -static char * bow_getsavpth(void) { +char const * bow_getsavpth(void) { char const * hmdir = flux_hmdir(); zap_sz hmdirlen; char const * filnm; zap_sz filnmlen; if (hmdir == nullptr) { bow_log("unable to get home directory"); - bow_setstr(hmdir,hmdirlen,"./"); + bow_setstrlen(hmdir,hmdirlen,"./"); } else hmdirlen = zap_strlen(hmdir); - bow_setstr(filnm,filnmlen,".save.bowshock"); + bow_setstrlen(filnm,filnmlen,".save.bowshock"); zap_sz pthsz = hmdirlen+filnmlen+0x2u; char * pth = malloc(pthsz); if (pth == nullptr) { bow_log("unable to allocate memory"); - bow_quit(bow_stat_err); + bow_abrt(); } zap_cp(pth,hmdir,hmdirlen); pth += hmdirlen; @@ -35,13 +35,3 @@ static char * bow_getsavpth(void) { pth -= pthsz; return pth; } - -void bow_initdat(bow_playdat * const playdatptr,char const * * const savpthptr) { - bow_log("initialising data"); - char const * savpth = bow_getsavpth(); - bow_playdat playdat; - bow_cont(savpth,&playdat); - bow_gendat(&playdat); - *savpthptr = savpth; - zap_cp(playdatptr,&playdat,sizeof (playdat)); -} diff --git a/bowshock/source/bs/help.c b/bowshock/source/bs/help.c new file mode 100644 index 0000000..d9dd430 --- /dev/null +++ b/bowshock/source/bs/help.c @@ -0,0 +1,22 @@ +// Copyright 2022-2023 Gabriel Jensen. + +#include <bow/bs.h> + +#include <inttypes.h> +#include <stdlib.h> + +void bow_help(char const * const prognm) { + bow_rawlog( + "\n" + "bowshock %" PRIXLEAST64 ".%" PRIXLEAST64 ".%" PRIXLEAST64 "\n" + "Copyright 2022-2023 Gabriel Jensen.\n" + "\n" + "Usage: %s <options> [savefile]\n" + "\n" + "Options:\n" + " --help Print help screen.\n" + " --reset Don't load save file.\n" + "\n", + bow_vermaj,bow_vermin,bow_verpat,prognm); + exit(EXIT_SUCCESS); +} diff --git a/bowshock/source/bs/init.c b/bowshock/source/bs/init.c index 323b12a..a001e6e 100644 --- a/bowshock/source/bs/init.c +++ b/bowshock/source/bs/init.c @@ -1,6 +1,7 @@ -#define bow_sym "init" +// Copyright 2022-2023 Gabriel Jensen. #include <bow/gfx.h> +#include <bow/lgc.h> #include <bow/sav.h> #include <inttypes.h> @@ -9,35 +10,34 @@ #include <time.h> #include <zap/str.h> -bow_stat bow_loop(bow_playdat * playdat); +[[noreturn]] void bow_init(int argc,char const * const * argv); sig_atomic_t volatile bow_gotintr; -static bool bow_getquot(char const * * quot,char const * * src,zap_i8 id); - static void bow_intrhandl(int const sig) { signal(sig,bow_intrhandl); // Ignore the return value. bow_gotintr = 0x1; } -int main(int const argc,char const * const * argv) { - char const * const prognm = argv[0x0u]; - if (argc > 0x1) { - char const * const * const stop = argv+(zap_sz)argc; - while (argv++ != stop) { - char const * const arg = *argv; - if (zap_streq(arg,"--help")) { - bow_rawlog( - "\n" - "bowshock %" PRIXLEAST64 ".%" PRIXLEAST64 "\n" - "Copyright 2022-2023 Gabriel Jensen\n" - "\n" - "Usage: %s [savefile]\n" - "\n", - bow_vermaj,bow_vermin,prognm); - return EXIT_SUCCESS; +void bow_init(int const argc,char const * const * argv) { + char const * const prognm = *argv; + char const * savpth; + bool hassavpth = false; + bool skipsavld = false; + if (argc >= 0x2) { + char const * const * const stop = (argv++)+(zap_sz)argc; + for (;argv != stop;++argv) { + char const * param = *argv; + if (param[0x0u] == '-' && param[0x1u] == '-') { + param += 0x2u; + if (zap_streq(param,"help")) bow_help(prognm); + else if (zap_streq(param,"reset")) skipsavld = true; + else bow_logerr("invalid parameter \"%s\"",param); + continue; } - bow_logerr("invalid parameter \"%s\"",arg); + // Else: Interpret it as a save path; + savpth = param; + hassavpth = true; } } srand((unsigned int)time(nullptr)); @@ -51,159 +51,25 @@ int main(int const argc,char const * const * argv) { } bow_rawlog("\n%s\n\u2014 %s\n\n",quot,src); } - bow_rawlog("\x1B[0m\x1B[1mBowshock\x1B[0m %" PRIXLEAST64 ".%" PRIXLEAST64 " \u2013 Copyright 2022\u20102023 \x1B[1mGabriel Jensen\x1B[0m.\n\n",bow_vermaj,bow_vermin); + bow_rawlog("\x1B[0m\x1B[1mBowshock\x1B[0m %" PRIXLEAST64 ".%" PRIXLEAST64 ".%" PRIXLEAST64 " \u2013 Copyright 2022\u20102023 \x1B[1mGabriel Jensen\x1B[0m.\n\n",bow_vermaj,bow_vermin,bow_verpat); bow_log("initialising"); bow_dbglog("debug mode is enabled"); bow_gotintr = 0x0; if (signal(SIGINT,bow_intrhandl) == SIG_ERR) { bow_log("unable to set signal handler"); - bow_quit(bow_stat_err); + bow_abrt(); } - char const * savpth; + if (!hassavpth) savpth = bow_getsavpth(); bow_playdat playdat; - bow_initdat(&playdat,&savpth); - bow_initgfx(); - bow_loop(&playdat); + bow_cont(savpth,&playdat,skipsavld); + bow_gfxdat gfxdat; + bow_initgfx(&gfxdat); + bow_loop(&gfxdat,&playdat); bow_sav(savpth,&playdat); - bow_quit(bow_stat_ok); + if (!hassavpth) free((char *)savpth); + bow_quit(&gfxdat,bow_stat_ok); } -static bool bow_getquot(char const * * const quot,char const * * src,zap_i8 const id) { - switch (id) { - default: - return true; - case 0x0u: - *quot = "You gotta be heaven to see heaven."; - *src = "Jim Carrey"; - break; - case 0x1u: - *quot = "Though it's the end of the world, don't blame yourself, now."; - *src = "Porter Robinson"; - break; - case 0x2u: - *quot = "The future will be better tomorrow."; - *src = "Dan Quayle"; - break; - case 0x3u: - *quot = "Sir, an equation has no meaning for me unless it expresses a thought of god."; - *src = "Srinivasa Ramanujan Aiyangar"; - break; - case 0x4u: - *quot = "Amīcus Platō amīcus Aristotelēs magis amīca vēritās."; - *src = "Isaac Newton"; - break; - case 0x5u: - *quot = "I have studied these things \u2013 you have not."; - *src = "Isaac Newton"; - break; - case 0x6u: - *quot = "La construction d'une machine propre à exprimer tous les sons de nos paroles, avec toutes les articulations, serait sans doute une décourverte bien importante.\n... La chose ne me paraît pas impossible."; - *src = "Leonhard Euler"; - break; - case 0x7u: - *quot = "In mathematics, you don't understand things, you just get used to them."; - *src = "John von Neumann"; - break; - case 0x8u: - *quot = "Being a language, mathematics may be used not only to inform, but also \u2013 among other things \u2013 to seduce."; - *src = "Benoît B. Mandelbrot"; - break; - case 0x9u: - *quot = "The real question is not whether machines think, but whether men do."; - *src = "Burrhus Frederic Skinner"; - break; - case 0xAu: - *quot = "Those who are not shocked when they first come across quantum theory cannot possibly have understood it."; - *src = "Niels Henrik David Bohr"; - break; - case 0xBu: - *quot = "Every sentence I utter must be understood not as an affirmation, but as a question."; - *src = "Niels Henrik David Bohr"; - break; - case 0xCu: - *quot = "We will now discuss in a little more detail the struggle for existence."; - *src = "Charles Robert Darwin"; - break; - case 0xDu: - *quot = "Nam et ipsa scientia potestas est."; - *src = "Francis Bacon"; - break; - case 0xEu: - *quot = "We don't know a millionth of one percent about anything."; - *src = "Thomas Alva Edison"; - break; - case 0xFu: - *quot = "My goal is simple. It is a complete understanding of the universe, why it is as it is, and why it exists at all."; - *src = "Stephen William Hawking"; - break; - case 0x10u: - *quot = "Equipped with his five senses, man explores the universe around him and calls the adventure Science."; - *src = "Edwin Powell Hubble"; - break; - case 0x11u: - *quot = "I can say this: I believe that the human mind, or even the mind of a cat, is more interesting in its complexity than an entire galaxy if it is devoid of life."; - *src = "Martin Gardner"; - break; - case 0x12u: - *quot = "I'm always right. This time I'm just more right than usual."; - *src = "Linus Benedict Torvalds"; - break; - case 0x13u: - *quot = "I'm an instant star. Just add water and stir."; - *src = "David Robert Jones"; - break; - case 0x14u: - *quot = "Don't waste the Earth \u2013 it is our jewel!"; - *src = "Buzz Eugene Aldrin"; - break; - case 0x15u: - *quot = "I think we're going to the moon because it's in the nature of the human being to face challenges."; - *src = "Neil Alden Armstrong"; - break; - case 0x16u: - *quot = "A hacker is someone who enjoys playful cleverness \u2013 not necessarily with computers."; - *src = "Richard Matthew Stallman"; - break; - case 0x17u: - *quot = "So Einstein was wrong when he said \"God noes not play dice.\". Consideration of black holes suggests, not only that God does play dice, but that he sometimes confuses us by throwing them where they can't be seen."; - *src = "Stephen William Hawking"; - break; - case 0x18u: - *quot = "I'm a blackstar."; - *src = "David Robert Jones"; - break; - case 0x19u: - *quot = "Sooner or later, we must expand life beyond our little blue mud ball \u2013 or go extinct."; - *src = "Elon Reeve Musk"; - break; - case 0x1Au: - *quot = "I would like to die on Mars \u2013 just not on impact."; - *src = "Elon Reeve Musk"; - break; - case 0x1Bu: - *quot = "The web does not just connect computers; it connects people."; - *src = "Timothy John Berners-Lee"; - break; - case 0x1Cu: - *quot = "Can digital computers think?"; - *src = "Alan Mathison Turing"; - break; - case 0x1Du: - *quot = "That's one small step for a man, one giant leap for mankind."; - *src = "Neil Alden Armstrong"; - break; - case 0x1Eu: - *quot = "If you think it's simple, then you have misunderstood the problem."; - *src = "Bjarne Stroustrup"; - break; - case 0x1Fu: - *quot = "Controlling complexity is the essence of computer programming."; - *src = "Brian Wilson Kerningham"; - break; - case 0x20u: - *quot = "I have always wished for my computer to be as easy to use as my telephone. My wish has come true because I can no longer figure out how to use my telephone."; - *src = "Bjarne Stroustrup"; - break; - } - return false; +int main(int const argc,char const * const * argv) { + bow_init(argc,argv); } diff --git a/bowshock/source/bs/loop.c b/bowshock/source/bs/loop.c index 8693510..d8339ea 100644 --- a/bowshock/source/bs/loop.c +++ b/bowshock/source/bs/loop.c @@ -1,4 +1,4 @@ -#define bow_sym "loop" +// Copyright 2022-2023 Gabriel Jensen. #include <bow/gfx.h> #include <bow/lgc.h> @@ -9,16 +9,14 @@ #include <inttypes.h> #include <math.h> #include <stdlib.h> -#include <webp/encode.h> #include <zap/mem.h> -bow_stat bow_loop(bow_playdat * playdatptr) { +bow_stat bow_loop(bow_gfxdat * gfxdatptr,bow_playdat * playdatptr) { bow_log("entering main loop"); + bow_gfxdat gfxdat; bow_playdat playdat; + zap_cp(&gfxdat, gfxdatptr, sizeof (gfxdat)); zap_cp(&playdat,playdatptr,sizeof (playdat)); - zap_sz const imgsz = 0xFFFu * 0xFFFu * 0x3u; - zap_i8 * img = malloc(imgsz); - zap_fill(img,0x0u,imgsz); bow_objroot sysroot = { // For stellar bodies. .objs = nullptr, }; @@ -45,7 +43,7 @@ bow_stat bow_loop(bow_playdat * playdatptr) { .rotvel = { .x = 0x0p0, .y = 0x0p0, - .z = 0x3.243F'6A89p0, + .z = 0x0p0, }, .mass = 0x191930A5E75F0C191814000000p0, .startyp = bow_star_g, @@ -65,7 +63,7 @@ bow_stat bow_loop(bow_playdat * playdatptr) { .z = 0x0p0, }, .posvel = { - .x = 0x7652p0, + .x = 0x7652p0/bow_tmmod, .y = 0x0p0, .z = 0x0p0, }, @@ -78,47 +76,42 @@ bow_stat bow_loop(bow_playdat * playdatptr) { .wrldtyp = bow_wrld_rck, }; bow_obj * const ter = bow_addobj(&sysroot,&objtmp); - for (zap_i04 i = 0x0u;;) { + for (;;++playdat.tm) { + glfwPollEvents(); if (bow_gotintr) { bow_log("got interrupt"); - glfwSetWindowShouldClose(bow_gfxdat.win,GLFW_TRUE); + glfwSetWindowShouldClose(gfxdat.win,GLFW_TRUE); } - if (glfwWindowShouldClose(bow_gfxdat.win)) break; + if (glfwWindowShouldClose(gfxdat.win)) break; // Calculate gravitations: - bow_gravobjs(&sysroot); + //bow_gravobjs(&sysroot); + bow_grav(ter,sol); // Move objects: bow_mvobjs(sysroot.objs); - // Plot objects: - { - constexpr double frm = 0x22'3F8B'93C0p0*0x3p0; - zap_i04 const solposx = (zap_i04)(( sol->pos.x+frm/0x2p0)/frm*0xFFFp0); - zap_i04 const solposy = (zap_i04)((-sol->pos.y+frm/0x2p0)/frm*0xFFFp0); - zap_i04 const terposx = (zap_i04)(( ter->pos.x+frm/0x2p0)/frm*0xFFFp0); - zap_i04 const terposy = (zap_i04)((-ter->pos.y+frm/0x2p0)/frm*0xFFFp0); - if ((solposx <= 0xFFFu) && (solposy <= 0xFFFu) && (terposx <= 0xFFFu) && (terposy <= 0xFFFu)) { - zap_sz const solpos = (solposy*0xFFFu+solposx)*0x3u; - zap_sz const terpos = (terposy*0xFFFu+terposx)*0x3u; - img[solpos+0x0u] = 0xFFu; - img[terpos+0x1u] = 0xFFu; - } - } - // Check tick number: - if (i++ == 0x1E18557u * bow_tmmod) break; // One Gregorian year + // Render: + int frmbufw; + int frmbufh; + glfwGetFramebufferSize(gfxdat.win,&frmbufw,&frmbufh); + GLdouble const vrat = (GLdouble)frmbufh/(GLdouble)frmbufw; + glViewport(0x0,0x0,frmbufw,frmbufh); + glOrtho(-0x1p0,0x1p0,-0x1p0,0x1p0,0x0p0,0x0p0); + double const frm = 0x22'3F8B'93C0p0*0x3p0/0x2p0; + GLfloat const solposx = (GLfloat)(sol->pos.x/frm); + GLfloat const solposy = (GLfloat)(sol->pos.y/frm/vrat); + GLfloat const terposx = (GLfloat)(ter->pos.x/frm); + GLfloat const terposy = (GLfloat)(ter->pos.y/frm/vrat); + glClear(GL_COLOR_BUFFER_BIT); + glPointSize(0x4p0); + glBegin(GL_POINTS); + glColor3f(0x1p0,0x0p0,0x0p0); + glVertex2f(solposx,solposy); + glColor3f(0x0p0,0x1p0,0x0p0); + glVertex2f(terposx,terposy); + glEnd(); + glfwSwapBuffers(gfxdat.win); } bow_freeobjs(&sysroot); - { - zap_i8 * enc; - zap_sz const enclen = WebPEncodeLosslessRGB((void *)img,0xFFFu,0xFFFu,0xFFFu*0x3u,&enc); - flux_fil * imgfil; - flux_err err = flux_mkfil(&imgfil,"img.webp",0644u); - if (err == flux_err_exist) { - flux_op(&imgfil,"img.webp",flux_md_wr,flux_disc); - } - flux_wr(imgfil,enc,enclen); - flux_cl(imgfil); - WebPFree(enc); - } - free(img); + zap_cp(gfxdatptr, &gfxdat, sizeof (gfxdat)); zap_cp(playdatptr,&playdat,sizeof (playdat)); return bow_stat_ok; } diff --git a/bowshock/source/bs/quit.c b/bowshock/source/bs/quit.c index 0e7f4ae..08c19b2 100644 --- a/bowshock/source/bs/quit.c +++ b/bowshock/source/bs/quit.c @@ -1,4 +1,4 @@ -#define bow_sym "quit" +// Copyright 2022-2023 Gabriel Jensen. #include <bow/gfx.h> #include <bow/info.h> @@ -6,15 +6,16 @@ #include <GLFW/glfw3.h> #include <stddef.h> #include <stdlib.h> +#include <zap/mem.h> -void bow_quit(bow_stat const stat) { +void bow_quit(bow_gfxdat * gfxdatptr,bow_stat const stat) { bow_log("quitting"); - glfwDestroyWindow(bow_gfxdat.win); + bow_gfxdat gfxdat; + zap_cp(&gfxdat,gfxdatptr,sizeof (gfxdat)); + glfwDestroyWindow(gfxdat.win); glfwTerminate(); int sysstat; switch (stat) { - default: - unreachable(); case bow_stat_err: sysstat = EXIT_FAILURE; break; diff --git a/bowshock/source/gfx/drw.c b/bowshock/source/gfx/drw.c new file mode 100644 index 0000000..33cb159 --- /dev/null +++ b/bowshock/source/gfx/drw.c @@ -0,0 +1,9 @@ +// Copyright 2022-2023 Gabriel Jensen. + +#include <bow/gfx.h> +#include <bow/lgc.h> + +#include <GLFW/glfw3.h> + +/*void bow_drw(bow_gfxdat * gfxdatptr,bow_objroot * const sys,bow_objroot * const objs) { +}*/ diff --git a/bowshock/source/gfx/initgfx.c b/bowshock/source/gfx/initgfx.c index 3ecbcc3..95174e2 100644 --- a/bowshock/source/gfx/initgfx.c +++ b/bowshock/source/gfx/initgfx.c @@ -1,28 +1,32 @@ -#define bow_sym "initgfx" +// Copyright 2022-2023 Gabriel Jensen. #include <bow/gfx.h> #include <GLFW/glfw3.h> +#include <zap/mem.h> -typeof (bow_gfxdat) bow_gfxdat; - -void bow_initgfx(void) { +void bow_initgfx(bow_gfxdat * const datptr) { bow_log("initialising graphics"); + bow_gfxdat dat; + bow_dbglog("initialising glfw"); if (!glfwInit()) { bow_logerr("unable to initialise glfw"); - bow_quit(bow_stat_err); + bow_abrt(); } + bow_dbglog("initialising window"); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,0x3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,0x0); - GLFWwindow * win = glfwCreateWindow(0x300,0x200,"Bowshock",glfwGetPrimaryMonitor(),nullptr); + glfwWindowHint(GLFW_DECORATED, GLFW_FALSE); + GLFWwindow * win = glfwCreateWindow(0x300,0x200,"Bowshock",nullptr,nullptr); if (win == nullptr) { bow_logerr("unable to open window"); - bow_quit(bow_stat_err); + bow_abrt(); } glfwMakeContextCurrent(win); - int frmbufw; - int frmbufh; - glfwGetFramebufferSize(win,&frmbufw,&frmbufh); - glViewport(0x0,0x0,frmbufw,frmbufh); - bow_gfxdat.win = win; + glfwSwapInterval(0x1); + //glClearColor(0x1.6D6D6D6Ep-1,0x1.81818182p-4,0x1.9999999Ap-3,0x1p0); + glClearColor(0x0p0,0x0p0,0x0p0,0x1p0); + glClear(GL_COLOR_BUFFER_BIT); + dat.win = win; + zap_cp(datptr,&dat,sizeof (dat)); } diff --git a/bowshock/source/info/objtypstr.c b/bowshock/source/info/objtypstr.c index 719634a..9a53a87 100644 --- a/bowshock/source/info/objtypstr.c +++ b/bowshock/source/info/objtypstr.c @@ -1,4 +1,4 @@ -#define bow_sym "objtypstr" +// Copyright 2022-2023 Gabriel Jensen. #include <bow/info.h> @@ -7,8 +7,6 @@ char const * bow_objtypstr(bow_objtyp const typ) { char const * str; switch (typ) { - default: - unreachable(); case bow_objtyp_can: str = "canister"; break; diff --git a/bowshock/source/info/shipmass.c b/bowshock/source/info/shipmass.c index d7c03f4..2bad462 100644 --- a/bowshock/source/info/shipmass.c +++ b/bowshock/source/info/shipmass.c @@ -1,4 +1,4 @@ -#define bow_sym "shipmass" +// Copyright 2022-2023 Gabriel Jensen. #include <bow/info.h> diff --git a/bowshock/source/lgc/addobj.c b/bowshock/source/lgc/addobj.c index 375fcfc..2a36667 100644 --- a/bowshock/source/lgc/addobj.c +++ b/bowshock/source/lgc/addobj.c @@ -1,4 +1,4 @@ -#define bow_sym "addobj" +// Copyright 2022-2023 Gabriel Jensen. #include <bow/info.h> #include <bow/lgc.h> @@ -11,7 +11,7 @@ bow_obj * bow_addobj(bow_objroot * const root,bow_obj const * const objval) { bow_obj * const obj = malloc(sizeof (bow_obj)); if (obj == nullptr) { bow_dbglog("unable to allocate memory for object"); - bow_quit(bow_stat_err); + bow_abrt(); } zap_cp(obj,objval,sizeof (bow_obj)); obj->next = root->objs; diff --git a/bowshock/source/lgc/freeobjs.c b/bowshock/source/lgc/freeobjs.c index 66b3435..e410559 100644 --- a/bowshock/source/lgc/freeobjs.c +++ b/bowshock/source/lgc/freeobjs.c @@ -1,4 +1,4 @@ -#define bow_sym "freeobjs" +// Copyright 2022-2023 Gabriel Jensen. #include <bow/info.h> #include <bow/lgc.h> @@ -8,12 +8,11 @@ void bow_freeobjs(bow_objroot const * const root) { bow_dbglog("freeing objects"); - bow_obj * obj = root->objs; + bow_obj * obj; bow_obj * next; - while (obj != nullptr) { + for (obj = root->objs;obj != nullptr;obj = next) { bow_dbglog("freeing object of type %s",bow_objtypstr(obj->typ)); next = obj->next; free(obj); - obj = next; } } diff --git a/bowshock/source/lgc/grav.c b/bowshock/source/lgc/grav.c index 848aabf..3c4e394 100644 --- a/bowshock/source/lgc/grav.c +++ b/bowshock/source/lgc/grav.c @@ -1,4 +1,4 @@ -#define bow_sym "grav" +// Copyright 2022-2023 Gabriel Jensen. #include <bow/lgc.h> @@ -13,7 +13,7 @@ void bow_grav(bow_obj * obj0ptr,bow_obj * obj1ptr) { double const distx = obj1.pos.x-obj0.pos.x; double const disty = obj1.pos.y-obj0.pos.y; double const distz = obj1.pos.z-obj0.pos.z; - double const dist = sqrt(distx *distx+disty*disty+distz*distz); + double const dist = sqrt(distx*distx+disty*disty+distz*distz); double const angy = atan2(disty,distx); double const angz = atan2(distz,distx); double acc0 = bow_gravconst/pow(dist,2.0); @@ -40,7 +40,6 @@ void bow_grav(bow_obj * obj0ptr,bow_obj * obj1ptr) { void bow_gravobjs(bow_objroot const * const root) { for (bow_obj * obj0 = root->objs;obj0 != nullptr;obj0 = obj0->next) - for (bow_obj * obj1 = obj0->next;obj1 != nullptr;obj1 = obj1->next) { + for (bow_obj * obj1 = obj0->next;obj1 != nullptr;obj1 = obj1->next) bow_grav(obj0,obj1); - } } diff --git a/bowshock/source/lgc/mv.c b/bowshock/source/lgc/mv.c index 175b076..a039913 100644 --- a/bowshock/source/lgc/mv.c +++ b/bowshock/source/lgc/mv.c @@ -1,4 +1,4 @@ -#define bow_sym "mv" +// Copyright 2022-2023 Gabriel Jensen. #include <bow/lgc.h> diff --git a/bowshock/source/sav/cont.c b/bowshock/source/sav/cont.c index 3eb1580..4cdadc5 100644 --- a/bowshock/source/sav/cont.c +++ b/bowshock/source/sav/cont.c @@ -1,5 +1,6 @@ -#define bow_sym "cont" +// Copyright 2022-2023 Gabriel Jensen. +#include <bow/lgc.h> #include <bow/sav.h> #include <flux/io.h> @@ -26,7 +27,11 @@ static void bow_decsav(bow_savdat * buf,zap_i8 * dat) { dat = zap_cp(&buf->shiprotvelz,dat,0x8u).src; } -void bow_cont(char const * const pth,bow_playdat * const playdatptr) { +void bow_cont(char const * const pth,bow_playdat * const playdatptr,bool const skipld) { + if (skipld) { + bow_log("skipping save load"); + goto new; + } bow_log("loading save file at \"%s\"",pth); flux_fil * fil; flux_err err = flux_op(&fil,pth,flux_md_rd,flux_keep); @@ -38,9 +43,7 @@ void bow_cont(char const * const pth,bow_playdat * const playdatptr) { err = flux_rd(rawdat,fil,bow_savlen,nullptr); if (err) { flux_cl(fil); - if (err == flux_err_eof) { - bow_logerr("corrupt save file at \"%s\"",pth); - } + if (err == flux_err_eof) bow_logerr("corrupt save file at \"%s\"",pth); else bow_logerr("unable to read file at \"%s\"",pth); goto new; } @@ -107,5 +110,6 @@ new: }; bow_log("generated commander %s",playdat.nm); ret: + bow_gendat(&playdat); zap_cp(playdatptr,&playdat,sizeof (playdat)); } diff --git a/bowshock/source/sav/sav.c b/bowshock/source/sav/sav.c index 451522e..c99fecf 100644 --- a/bowshock/source/sav/sav.c +++ b/bowshock/source/sav/sav.c @@ -1,5 +1,6 @@ -#define bow_sym "sav" +// Copyright 2022-2023 Gabriel Jensen. +#include <bow/lgc.h> #include <bow/sav.h> #include <flux/io.h> @@ -32,12 +33,12 @@ void bow_sav(char const * const pth,bow_playdat const * const playdat) { if (err) { if (err != flux_err_exist) { bow_logerr("unable to open save file \"%s\"",pth); - bow_quit(bow_stat_err); + bow_abrt(); } err = flux_op(&fil,pth,flux_md_wr,flux_disc); if (err) { bow_logerr("unable to create save file \"%s\"",pth); - bow_quit(bow_stat_err); + bow_abrt(); } } zap_i8 dat[bow_savlen]; |