main.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. /*
  2. --------------------------------------------------
  3. James William Fletcher (github.com/mrbid)
  4. February 2023
  5. --------------------------------------------------
  6. C & SDL / OpenGL ES2 / GLSL ES
  7. Colour Converter: https://www.easyrgb.com
  8. */
  9. #include <time.h>
  10. #include <SDL2/SDL.h>
  11. #include <SDL2/SDL_opengles2.h>
  12. #include "esAux4.h"
  13. #include "assets/res.h"
  14. #include "assets/scene.h"
  15. #include "assets/dynamic.h"
  16. #include "assets/minball.h"
  17. #define uint GLuint
  18. #define sint GLint
  19. #define f32 GLfloat
  20. #define forceinline __attribute__((always_inline)) inline
  21. //*************************************
  22. // globals
  23. //*************************************
  24. char appTitle[] = "Snowling";
  25. SDL_Window* wnd;
  26. SDL_GLContext glc;
  27. SDL_Surface* s_icon = NULL;
  28. Uint32 winw = 1024, winh = 768;
  29. f32 aspect, t = 0.f;
  30. // render state id's
  31. GLint projection_id;
  32. GLint modelview_id;
  33. GLint position_id;
  34. GLint lightpos_id;
  35. GLint color_id;
  36. GLint opacity_id;
  37. GLint normal_id;
  38. GLint texcoord_id;
  39. GLint sampler_id;
  40. // render state matrices
  41. mat projection;
  42. mat view;
  43. mat model;
  44. mat modelview;
  45. // render state inputs
  46. vec lightpos = {0.f, 7.f, 0.f};
  47. // models
  48. ESModel mdlPlane;
  49. GLuint tex_skyplane;
  50. ESModel mdlScene;
  51. ESModel mdlDynamic;
  52. ESModel mdlMinball;
  53. uint bindstate = 0;
  54. // simulation / game vars
  55. uint ground = 0; // current round number
  56. uint score = 0; // total score
  57. uint rscore = 0; // round score
  58. uint penalty = 0; // penalty count
  59. uint state = 0; // game state
  60. double s0lt = 0; // time offset controls ball swing offset
  61. vec bp; // ball position
  62. f32 stepspeed = 0.f; // ball speed
  63. f32 hardness = 0.f; // ball hardness
  64. //*************************************
  65. // utility functions
  66. //*************************************
  67. void timestamp(char* ts){const time_t tt = time(0);strftime(ts, 16, "%H:%M:%S", localtime(&tt));}
  68. forceinline f32 f32Time(){return ((f32)SDL_GetTicks())*0.001f;}
  69. //*************************************
  70. // gl functions
  71. //*************************************
  72. void doPerspective()
  73. {
  74. glViewport(0, 0, winw, winh);
  75. aspect = (f32)winw / (f32)winh;
  76. mIdent(&projection);
  77. mPerspective(&projection, 60.0f, aspect, 0.1f, 80.f);
  78. glUniformMatrix4fv(projection_id, 1, GL_FALSE, (f32*)&projection.m[0][0]);
  79. }
  80. //*************************************
  81. // generation functions
  82. //*************************************
  83. SDL_Surface* surfaceFromData(const Uint32* data, Uint32 w, Uint32 h)
  84. {
  85. SDL_Surface* s = SDL_CreateRGBSurfaceWithFormat(0, w, h, 32, SDL_PIXELFORMAT_RGBA32);
  86. memcpy(s->pixels, data, s->pitch*h);
  87. return s;
  88. }
  89. void blotColour2(f32 r, f32 g, f32 b, f32 rad)
  90. {
  91. const GLushort rci = esRand(0, dynamic_numvert-1) * 3;
  92. const vec tv = (vec){dynamic_vertices[rci], dynamic_vertices[rci+1], dynamic_vertices[rci+2]};
  93. if(tv.x > 9.6f) // not too close to spawn
  94. return;
  95. const GLushort tc = (dynamic_numvert-1)*3;
  96. for(GLushort i = 0; i < tc; i += 3)
  97. {
  98. const vec nv = (vec){dynamic_vertices[i], dynamic_vertices[i+1], dynamic_vertices[i+2]};
  99. if(vDist(tv, nv) < rad)
  100. {
  101. dynamic_colors[i] = r;
  102. dynamic_colors[i+1] = g;
  103. dynamic_colors[i+2] = b;
  104. }
  105. }
  106. }
  107. void blotColour(f32 r, f32 g, f32 b, GLushort streak)
  108. {
  109. const GLushort rci = esRand(0, dynamic_numvert-1-streak) * 3;
  110. for(GLushort i = 0; i < streak; i++)
  111. {
  112. dynamic_colors[rci+(i*3)] = r;
  113. dynamic_colors[rci+(i*3)+1] = g;
  114. dynamic_colors[rci+(i*3)+2] = b;
  115. }
  116. }
  117. void gNewRound()
  118. {
  119. // count & log
  120. ground++;
  121. char strts[16];
  122. timestamp(&strts[0]);
  123. printf("[%s] STARTING ROUND %u - SCORE %u - PENALTY %u\n", strts, ground, score, penalty);
  124. if(ground > 1)
  125. {
  126. char title[256];
  127. sprintf(title, "ROUND %u - SCORE %u - PENALTY %u", ground, score, penalty);
  128. SDL_SetWindowTitle(wnd, title);
  129. }
  130. // reset
  131. const GLushort tc = dynamic_numvert*3;
  132. for(GLushort i = 0; i < tc; i++)
  133. dynamic_colors[i] = 1.f;
  134. // ice
  135. for(uint i = 0; i < 6; i++)
  136. {
  137. if(esRand(0, 1000) < 500)
  138. blotColour(0.f, 1.f, 1.f, 32);
  139. else
  140. blotColour2(0.f, 1.f, 1.f, esRandFloat(0.1f, 0.9f));
  141. }
  142. // boost
  143. for(uint i = 0; i < 6; i++)
  144. {
  145. if(esRand(0, 1000) < 500)
  146. blotColour(0.50196f, 0.00000f, 0.50196f, 6);
  147. else
  148. blotColour2(0.50196f, 0.00000f, 0.50196f, esRandFloat(0.1f, 0.3f));
  149. }
  150. // lava
  151. for(uint i = 0; i < 2; i++)
  152. {
  153. if(esRand(0, 1000) < 500)
  154. blotColour(0.81176f, 0.06275f, 0.12549f, 6);
  155. else
  156. blotColour2(0.81176f, 0.06275f, 0.12549f, esRandFloat(0.1f, 0.3f));
  157. }
  158. // rebind
  159. glBindBuffer(GL_ARRAY_BUFFER, mdlDynamic.cid);
  160. glBufferData(GL_ARRAY_BUFFER, sizeof(dynamic_colors), dynamic_colors, GL_STATIC_DRAW);
  161. }
  162. uint checkCollisions(const f32 bpy)
  163. {
  164. // static double ccl = 0;
  165. // if(t < ccl)
  166. // return 0;
  167. // else
  168. // ccl = t + 0.05; // limit checkCollisions() execution frequency
  169. vec lbp = bp;
  170. lbp.z -= bpy;
  171. static double tt1 = 0, tt2 = 0, tt3 = 0;
  172. static const GLushort tc = (dynamic_numvert-1)*3;
  173. for(GLushort i = 0; i < tc; i+=3)
  174. {
  175. if(t > tt1 && dynamic_colors[i] == 0.f && dynamic_colors[i+1] == 1.f && dynamic_colors[i+2] == 1.f)
  176. {
  177. const vec cp = {dynamic_vertices[i], dynamic_vertices[i+1], dynamic_vertices[i+2]};
  178. if(vDistSq(cp, lbp) < 0.0169f)
  179. {
  180. hardness += 1.f;
  181. char strts[16];
  182. timestamp(&strts[0]);
  183. printf("[%s] hit ice: %u\n", strts, (uint)hardness);
  184. tt1 = t+0.3; // freq limiter
  185. return 1;
  186. }
  187. }
  188. else if(t > tt2 && dynamic_colors[i] == 0.50196f && dynamic_colors[i+1] == 0.f && dynamic_colors[i+2] == 0.50196f)
  189. {
  190. const vec cp = {dynamic_vertices[i], dynamic_vertices[i+1], dynamic_vertices[i+2]};
  191. if(vDistSq(cp, lbp) < 0.0169f)
  192. {
  193. stepspeed *= 3.4f;
  194. char strts[16];
  195. timestamp(&strts[0]);
  196. printf("[%s] hit boost: %.1f\n", strts, stepspeed);
  197. tt2 = t+0.3; // freq limiter
  198. return 2;
  199. }
  200. }
  201. else if(t > tt3 && dynamic_colors[i] == 0.81176f && dynamic_colors[i+1] == 0.06275f && dynamic_colors[i+2] == 0.12549f)
  202. {
  203. const vec cp = {dynamic_vertices[i], dynamic_vertices[i+1], dynamic_vertices[i+2]};
  204. if(vDistSq(cp, lbp) < 0.0169f)
  205. {
  206. penalty++;
  207. char strts[16];
  208. timestamp(&strts[0]);
  209. printf("[%s] hit lava: %u\n", strts, penalty);
  210. tt3 = t+0.3; // freq limiter
  211. return 3;
  212. }
  213. }
  214. }
  215. return 0;
  216. }
  217. //*************************************
  218. // render functions
  219. //*************************************
  220. void rSkyPlane()
  221. {
  222. bindstate = 0;
  223. static mat skyplane_model = {0.f};
  224. static f32 la = 0;
  225. if(skyplane_model.m[0][0] == 0.f || la != aspect)
  226. {
  227. mIdent(&skyplane_model);
  228. mSetPos(&skyplane_model, (vec){-40.f, 0.f, 0.f});
  229. mRotY(&skyplane_model, -90.f*DEG2RAD);
  230. mRotX(&skyplane_model, -90.f*DEG2RAD);
  231. mScale(&skyplane_model, 40.f*aspect, 23.f*aspect, 0);
  232. la = aspect;
  233. }
  234. mMul(&modelview, &skyplane_model, &view);
  235. glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*)&modelview.m[0][0]);
  236. glBindBuffer(GL_ARRAY_BUFFER, mdlPlane.tid);
  237. glVertexAttribPointer(texcoord_id, 2, GL_FLOAT, GL_FALSE, 0, 0);
  238. glEnableVertexAttribArray(texcoord_id);
  239. glActiveTexture(GL_TEXTURE0);
  240. glBindTexture(GL_TEXTURE_2D, tex_skyplane);
  241. glUniform1i(sampler_id, 0);
  242. glBindBuffer(GL_ARRAY_BUFFER, mdlPlane.vid);
  243. glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  244. glEnableVertexAttribArray(position_id);
  245. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlPlane.iid);
  246. glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
  247. }
  248. void rStaticScene()
  249. {
  250. bindstate = 0;
  251. glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &view.m[0][0]);
  252. glUniform3f(lightpos_id, lightpos.x, lightpos.y, lightpos.z);
  253. glBindBuffer(GL_ARRAY_BUFFER, mdlScene.vid);
  254. glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  255. glEnableVertexAttribArray(position_id);
  256. glBindBuffer(GL_ARRAY_BUFFER, mdlScene.nid);
  257. glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  258. glEnableVertexAttribArray(normal_id);
  259. glBindBuffer(GL_ARRAY_BUFFER, mdlScene.cid);
  260. glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  261. glEnableVertexAttribArray(color_id);
  262. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlScene.iid);
  263. glDrawElements(GL_TRIANGLES, scene_numind, GL_UNSIGNED_SHORT, 0);
  264. }
  265. void rDynamicScene()
  266. {
  267. bindstate = 0;
  268. glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &view.m[0][0]);
  269. glUniform3f(lightpos_id, lightpos.x, lightpos.y, lightpos.z);
  270. glBindBuffer(GL_ARRAY_BUFFER, mdlDynamic.vid);
  271. glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  272. glEnableVertexAttribArray(position_id);
  273. glBindBuffer(GL_ARRAY_BUFFER, mdlDynamic.nid);
  274. glVertexAttribPointer(normal_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  275. glEnableVertexAttribArray(normal_id);
  276. glBindBuffer(GL_ARRAY_BUFFER, mdlDynamic.cid);
  277. glVertexAttribPointer(color_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  278. glEnableVertexAttribArray(color_id);
  279. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlDynamic.iid);
  280. glDrawElements(GL_TRIANGLES, dynamic_numind, GL_UNSIGNED_SHORT, 0);
  281. }
  282. void rMinballRGB(f32 x, f32 y, f32 z, f32 r, f32 g, f32 b, f32 s)
  283. {
  284. mIdent(&model);
  285. mSetPos(&model, (vec){x, y, z});
  286. if(s != 1.f)
  287. mScale(&model, s, s, s);
  288. mMul(&modelview, &model, &view);
  289. glUniformMatrix4fv(modelview_id, 1, GL_FALSE, (f32*) &modelview.m[0][0]);
  290. glUniform3f(color_id, r, g, b);
  291. glUniform3f(lightpos_id, lightpos.x, lightpos.y, lightpos.z);
  292. if(bindstate == 0)
  293. {
  294. glBindBuffer(GL_ARRAY_BUFFER, mdlMinball.vid);
  295. glVertexAttribPointer(position_id, 3, GL_FLOAT, GL_FALSE, 0, 0);
  296. glEnableVertexAttribArray(position_id);
  297. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mdlMinball.iid);
  298. bindstate = 1;
  299. }
  300. glDrawElements(GL_TRIANGLES, minball_numind, GL_UNSIGNED_SHORT, 0);
  301. }
  302. static inline void rMinball(f32 x, f32 y, f32 z, f32 s)
  303. {
  304. rMinballRGB(x, y, z, 1.f, 1.f, 1.f, s);
  305. }
  306. void rPin(f32 x, f32 y, f32 z, uint down)
  307. {
  308. if(down == 1)
  309. {
  310. rMinballRGB(x, y, z, 1.f, 0.f, 0.f, 2.2f);
  311. }
  312. else
  313. {
  314. rMinball(x, y, z, 2.2f);
  315. rMinball(x, y, z+0.14f, 1.45f);
  316. rMinball(x, y, z+0.24f, 0.9f);
  317. }
  318. }
  319. void rPinSet()
  320. {
  321. rPin(-0.9, 0.f, 0.f, rscore >= 1 ? 1 : 0);
  322. rPin(-1.2, -0.14f, 0.f, rscore >= 2 ? 1 : 0);
  323. rPin(-1.2, 0.14f, 0.f, rscore >= 3 ? 1 : 0);
  324. rPin(-1.5, 0.f, 0.f, rscore >= 4 ? 1 : 0);
  325. rPin(-1.5, -0.28f, 0.f, rscore >= 5 ? 1 : 0);
  326. rPin(-1.5, 0.28f, 0.f, rscore >= 6 ? 1 : 0);
  327. rPin(-1.8, -0.14f, 0.f, rscore >= 7 ? 1 : 0);
  328. rPin(-1.8, 0.14f, 0.f, rscore >= 8 ? 1 : 0);
  329. rPin(-1.8, -0.42f, 0.f, rscore >= 9 ? 1 : 0);
  330. rPin(-1.8, 0.42f, 0.f, rscore >= 10 ? 1 : 0);
  331. }
  332. //*************************************
  333. // interpolators and steppers for simulation
  334. //*************************************
  335. typedef struct { f32 y,z; } lt;
  336. static inline f32 lerp(lt* a, lt* b, f32 y)
  337. {
  338. return a->z + (((y - a->y) / (b->y - a->y)) * (b->z - a->z));
  339. }
  340. lt hlt[25];
  341. f32 getHeight(f32 y)
  342. {
  343. y = fabs(y);
  344. for(uint i = 0; i < 24; i++)
  345. if(y >= hlt[i].y && y < hlt[i+1].y)
  346. return lerp(&hlt[i], &hlt[i+1], y);
  347. return -1.f; // bad times if this happens
  348. }
  349. static inline f32 smoothStepN(f32 v)
  350. {
  351. return v * v * (3.f - 2.f * v);
  352. }
  353. //*************************************
  354. // update & render
  355. //*************************************
  356. void main_loop()
  357. {
  358. //*************************************
  359. // time delta for interpolation
  360. //*************************************
  361. static f32 lt = 0;
  362. t = f32Time();
  363. const f32 dt = t-lt;
  364. lt = t;
  365. //*************************************
  366. // input handling
  367. //*************************************
  368. SDL_Event event;
  369. while(SDL_PollEvent(&event))
  370. {
  371. if(event.type == SDL_FINGERDOWN)
  372. {
  373. if(stepspeed == 0.f)
  374. {
  375. stepspeed = 0.5f + randf()*6.f;
  376. s0lt = t - randf()*x2PI;
  377. }
  378. }
  379. else if(event.type == SDL_KEYDOWN)
  380. {
  381. if(stepspeed == 0.f)
  382. {
  383. stepspeed = 0.5f + randf()*6.f;
  384. s0lt = t - randf()*x2PI;
  385. }
  386. }
  387. else if(event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT)
  388. {
  389. if(stepspeed == 0.f)
  390. {
  391. stepspeed = 0.5f + randf()*6.f;
  392. s0lt = t - randf()*x2PI;
  393. }
  394. }
  395. else if(event.type == SDL_QUIT)
  396. {
  397. SDL_GL_DeleteContext(glc);
  398. SDL_FreeSurface(s_icon);
  399. SDL_DestroyWindow(wnd);
  400. SDL_Quit();
  401. exit(0);
  402. }
  403. else if(event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESIZED)
  404. {
  405. winw = event.window.data1;
  406. winh = event.window.data2;
  407. doPerspective();
  408. }
  409. }
  410. //*************************************
  411. // camera control
  412. //*************************************
  413. static f32 camdist = -15.f;
  414. mIdent(&view);
  415. mSetPos(&view, (vec){0.f, -0.5f, camdist});
  416. mRotY(&view, 1.396263361f);
  417. mRotZ(&view, 1.570796371f);
  418. //*************************************
  419. // begin render
  420. //*************************************
  421. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  422. //*************************************
  423. // main render
  424. //*************************************
  425. // render sky plane
  426. shadeFullbrightT(&position_id, &projection_id, &modelview_id, &texcoord_id, &sampler_id);
  427. glUniformMatrix4fv(projection_id, 1, GL_FALSE, (f32*)&projection.m[0][0]);
  428. rSkyPlane();
  429. // render static and dynamic scenes
  430. shadeLambert3(&position_id, &projection_id, &modelview_id, &lightpos_id, &normal_id, &color_id, &opacity_id);
  431. glUniformMatrix4fv(projection_id, 1, GL_FALSE, (f32*) &projection.m[0][0]);
  432. glUniform1f(opacity_id, 1.0f);
  433. rStaticScene();
  434. rDynamicScene();
  435. // only rendering icospheres from here on out
  436. shadeLambert(&position_id, &projection_id, &modelview_id, &lightpos_id, &color_id, &opacity_id);
  437. glUniformMatrix4fv(projection_id, 1, GL_FALSE, (f32*) &projection.m[0][0]);
  438. glUniform1f(opacity_id, 1.0f);
  439. rPinSet();
  440. // simulate the bowling snowball & transition the game states
  441. static double s1lt = 0;
  442. static f32 x = 10.5f;
  443. static f32 ms = -1.f;
  444. if(state == 0) // bowl simulation state
  445. {
  446. if(stepspeed == 0.f)
  447. {
  448. rMinball(10.5f, 0.f-0.017f, 0.f, 1.f);
  449. }
  450. else
  451. {
  452. const f32 ddt = stepspeed * dt;
  453. x -= ddt;
  454. camdist += ddt;
  455. if(x <= -1.f)
  456. state = 1;
  457. const f32 h = sinf(t-s0lt)*(1.38f-((10.5f-x)*0.1f));
  458. f32 ns = (10.5f-x)*0.4f;
  459. if(ns < 1.f)
  460. ns = 1.f;
  461. lightpos.y = 7.f * smoothStepN(x*0.09523809701f);
  462. bp.x = x;
  463. bp.y = h-0.017f;
  464. const f32 ho = ((1.f-(bp.x*0.09523809701f))*0.08f);
  465. bp.z = getHeight(h) + ho;
  466. rMinballRGB(bp.x, bp.y, bp.z, 1.f-(hardness*0.22f), 1.f, 1.f, ns);
  467. if(checkCollisions(ho) == 3)
  468. state = 3;
  469. }
  470. }
  471. else if(state == 1) // pins knocked down freeze state
  472. {
  473. if(s1lt == 0)
  474. {
  475. // calc score
  476. if(hardness > 0.f)
  477. {
  478. const f32 fscore = ((hardness * stepspeed) * 0.3f)+0.5f;
  479. rscore = (uint)fscore;
  480. if(rscore == 0)
  481. {
  482. char strts[16];
  483. timestamp(&strts[0]);
  484. printf("[%s] ~~ PENALTY YOU FAILED TO KNOCK DOWN ANY PINS ~~\n", strts);
  485. penalty++;
  486. s1lt = t + 1;
  487. }
  488. else if(rscore >= 10)
  489. {
  490. rscore = 20;
  491. char strts[16];
  492. timestamp(&strts[0]);
  493. printf("[%s] !!! STRIKE !!! - ROUND %u - SCORE %u\n", strts, ground, rscore);
  494. s1lt = t + 3;
  495. }
  496. else
  497. {
  498. char strts[16];
  499. timestamp(&strts[0]);
  500. printf("[%s] ~~ ROUND %u SCORE %u ~~\n", strts, ground, rscore);
  501. s1lt = t + 1;
  502. }
  503. score += rscore;
  504. }
  505. else
  506. {
  507. char strts[16];
  508. timestamp(&strts[0]);
  509. printf("[%s] ~~ PENALTY YOU FAILED TO KNOCK DOWN ANY PINS ~~\n", strts);
  510. penalty++;
  511. s1lt = t + 1;
  512. }
  513. }
  514. if(t > s1lt)
  515. state = 2;
  516. }
  517. else if(state == 2) // new round state
  518. {
  519. // reset
  520. lightpos.y = 7.f;
  521. x = 10.5f;
  522. camdist = -15.f;
  523. stepspeed = 0.f;
  524. hardness = 0.f;
  525. state = 0;
  526. s1lt = 0;
  527. rscore = 0;
  528. ms = -1.f;
  529. gNewRound();
  530. }
  531. else if(state == 3) // hit lava, melting state (worst animation ever)
  532. {
  533. static f32 rsms = 0.f;
  534. if(ms == -1.f)
  535. {
  536. ms = (10.5f-x)*0.4f;
  537. rsms = 1.f/ms;
  538. if(ms < 1.f)
  539. ms = 1.f;
  540. }
  541. ms -= 1.f * dt;
  542. if(ms <= 0.f)
  543. state = 2;
  544. bp.z -= 0.075f * dt;
  545. rMinballRGB(bp.x, bp.y, bp.z, 1.f, 1.f - (1.f-ms*rsms), 1.f - (1.f-ms*rsms), ms);
  546. }
  547. //*************************************
  548. // swap buffers / display render
  549. //*************************************
  550. SDL_GL_SwapWindow(wnd);
  551. }
  552. //*************************************
  553. // Process Entry Point
  554. //*************************************
  555. int main(int argc, char** argv)
  556. {
  557. //*************************************
  558. // setup render context / window
  559. //*************************************
  560. int msaa = 16;
  561. if(argc >= 2){msaa = atoi(argv[1]);}
  562. printf("James William Fletcher (github.com/mrbid)\nIt's like a fruit machine, random chance, you click and it bowls a random bowl - a game of chance/luck.\n\nThe alpine background image was taken by Karl Köhler (https://unsplash.com/photos/landscape-photo-of-snowy-mountain-N_MXyBUV5hU).\n\n");
  563. if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_EVENTS) < 0)
  564. {
  565. printf("ERROR: SDL_Init(): %s\n", SDL_GetError());
  566. return 1;
  567. }
  568. SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
  569. SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, msaa);
  570. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
  571. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
  572. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
  573. wnd = SDL_CreateWindow(appTitle, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, winw, winh, SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
  574. while(wnd == NULL)
  575. {
  576. msaa--;
  577. if(msaa == 0)
  578. {
  579. printf("ERROR: SDL_CreateWindow(): %s\n", SDL_GetError());
  580. return 1;
  581. }
  582. SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, msaa);
  583. wnd = SDL_CreateWindow(appTitle, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, winw, winh, SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
  584. }
  585. SDL_GL_SetSwapInterval(1); // 0 for immediate updates, 1 for updates synchronized with the vertical retrace, -1 for adaptive vsync
  586. glc = SDL_GL_CreateContext(wnd);
  587. if(glc == NULL)
  588. {
  589. printf("ERROR: SDL_GL_CreateContext(): %s\n", SDL_GetError());
  590. return 1;
  591. }
  592. // set icon
  593. unsigned char* ipd = (unsigned char*)&icon_image2.pixel_data;
  594. srand(time(0));
  595. const uint r = esRand(0, 2);
  596. if(r == 0) {ipd = (unsigned char*)&icon_image.pixel_data;}
  597. else if(r == 1) {ipd = (unsigned char*)&icon_image1.pixel_data;}
  598. else {ipd = (unsigned char*)&icon_image2.pixel_data;}
  599. s_icon = surfaceFromData((Uint32*)ipd, 16, 16);
  600. SDL_SetWindowIcon(wnd, s_icon);
  601. // seed random
  602. srandf(time(0));
  603. srand(time(0));
  604. // gen height table
  605. hlt[0].y = 0; hlt[0].z = 0.016813;
  606. hlt[1].y = 0.102108; hlt[1].z = 0.018762;
  607. hlt[2].y = 0.191487; hlt[2].z = 0.024604;
  608. hlt[3].y = 0.280097; hlt[3].z = 0.034311;
  609. hlt[4].y = 0.367561; hlt[4].z = 0.047843;
  610. hlt[5].y = 0.453504; hlt[5].z = 0.065142;
  611. hlt[6].y = 0.537558; hlt[6].z = 0.086133;
  612. hlt[7].y = 0.619362; hlt[7].z = 0.110728;
  613. hlt[8].y = 0.698569; hlt[8].z = 0.138821;
  614. hlt[9].y = 0.774836; hlt[9].z = 0.170289;
  615. hlt[10].y = 0.847838; hlt[10].z = 0.205001;
  616. hlt[11].y = 0.917262; hlt[11].z = 0.242807;
  617. hlt[12].y = 0.982812; hlt[12].z = 0.283543;
  618. hlt[13].y = 1.04421; hlt[13].z = 0.327039;
  619. hlt[14].y = 1.10118; hlt[14].z = 0.373106;
  620. hlt[15].y = 1.15349; hlt[15].z = 0.421545;
  621. hlt[16].y = 1.20092; hlt[16].z = 0.472152;
  622. hlt[17].y = 1.24325; hlt[17].z = 0.524709;
  623. hlt[18].y = 1.28032; hlt[18].z = 0.57899;
  624. hlt[19].y = 1.31195; hlt[19].z = 0.634764;
  625. hlt[20].y = 1.33803; hlt[20].z = 0.69179;
  626. hlt[21].y = 1.35843; hlt[21].z = 0.749827;
  627. hlt[22].y = 1.37305; hlt[22].z = 0.808624;
  628. hlt[23].y = 1.38185; hlt[23].z = 0.867931;
  629. hlt[24].y = 1.38479; hlt[24].z = 0.927491;
  630. //*************************************
  631. // bind vertex and index buffers
  632. //*************************************
  633. // ***** BIND SKY PLANE *****
  634. f32 plane_vert[] = {1.000000,-1.000000,0.000000,
  635. -1.000000,1.000000,0.000000,
  636. -1.000000,-1.000000,0.000000,
  637. 1.000000,1.000000,0.000000};
  638. GLushort plane_indi[] = {0,1,2,0,3,1};
  639. esBindModel(&mdlPlane, plane_vert, 9, plane_indi, 6);
  640. f32 plane_texc[] = {1.f,1.f,
  641. 0.f,0.f,
  642. 0.f,1.f,
  643. 1.f,0.f};
  644. esBind(GL_ARRAY_BUFFER, &mdlPlane.tid, plane_texc, sizeof(plane_texc), GL_STATIC_DRAW);
  645. tex_skyplane = esLoadTexture(1469, 981, &alpinebg_tex[0], 1);
  646. // ***** BIND STATIC SCENE *****
  647. esBind(GL_ARRAY_BUFFER, &mdlScene.vid, scene_vertices, sizeof(scene_vertices), GL_STATIC_DRAW);
  648. esBind(GL_ARRAY_BUFFER, &mdlScene.nid, scene_normals, sizeof(scene_normals), GL_STATIC_DRAW);
  649. esBind(GL_ARRAY_BUFFER, &mdlScene.cid, scene_colors, sizeof(scene_colors), GL_STATIC_DRAW);
  650. esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlScene.iid, scene_indices, sizeof(scene_indices), GL_STATIC_DRAW);
  651. // ***** BIND DYNAMIC SCENE *****
  652. esBind(GL_ARRAY_BUFFER, &mdlDynamic.vid, dynamic_vertices, sizeof(dynamic_vertices), GL_STATIC_DRAW);
  653. esBind(GL_ARRAY_BUFFER, &mdlDynamic.nid, dynamic_normals, sizeof(dynamic_normals), GL_STATIC_DRAW);
  654. esBind(GL_ARRAY_BUFFER, &mdlDynamic.cid, dynamic_colors, sizeof(dynamic_colors), GL_STATIC_DRAW);
  655. esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlDynamic.iid, dynamic_indices, sizeof(dynamic_indices), GL_STATIC_DRAW);
  656. // ***** BIND MIN BALL *****
  657. esBind(GL_ARRAY_BUFFER, &mdlMinball.vid, minball_vertices, sizeof(minball_vertices), GL_STATIC_DRAW);
  658. esBind(GL_ELEMENT_ARRAY_BUFFER, &mdlMinball.iid, minball_indices, sizeof(minball_indices), GL_STATIC_DRAW);
  659. //*************************************
  660. // projection
  661. //*************************************
  662. doPerspective();
  663. //*************************************
  664. // compile & link shader program
  665. //*************************************
  666. makeFullbrightT();
  667. makeLambert();
  668. makeLambert3();
  669. //*************************************
  670. // configure render options
  671. //*************************************
  672. glEnable(GL_CULL_FACE);
  673. glEnable(GL_DEPTH_TEST);
  674. glClearColor(0.20000f, 0.34510f, 0.48627f, 0.0f);
  675. //*************************************
  676. // execute update / render loop
  677. //*************************************
  678. gNewRound();
  679. while(1){main_loop();}
  680. return 0;
  681. }