From 5bc319e1a26b7d07728a364bd6c86b5a32ddcc2d Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Tue, 29 Oct 2019 00:48:04 -0400 Subject: refactor sdl ui --- frontends/sdl/LICENSE.txt | 202 -------------- frontends/sdl/OpenSans-Regular.ttf | Bin 96932 -> 0 bytes frontends/sdl/audio/yea_0.wav | Bin 212984 -> 0 bytes frontends/sdl/audio/yea_1.wav | Bin 212984 -> 0 bytes frontends/sdl/audio/yea_2.wav | Bin 212984 -> 0 bytes frontends/sdl/audio/yea_3.wav | Bin 425912 -> 0 bytes frontends/sdl/cetris_sdl.h | 44 +++ frontends/sdl/data/LICENSE.txt | 202 ++++++++++++++ frontends/sdl/data/OpenSans-Regular.ttf | Bin 0 -> 96932 bytes frontends/sdl/data/yea_0.wav | Bin 0 -> 212984 bytes frontends/sdl/data/yea_1.wav | Bin 0 -> 212984 bytes frontends/sdl/data/yea_2.wav | Bin 0 -> 212984 bytes frontends/sdl/data/yea_3.wav | Bin 0 -> 425912 bytes frontends/sdl/meson.build | 10 +- frontends/sdl/sdl_ui.c | 463 +++++++++++++++----------------- 15 files changed, 465 insertions(+), 456 deletions(-) delete mode 100644 frontends/sdl/LICENSE.txt delete mode 100644 frontends/sdl/OpenSans-Regular.ttf delete mode 100644 frontends/sdl/audio/yea_0.wav delete mode 100644 frontends/sdl/audio/yea_1.wav delete mode 100644 frontends/sdl/audio/yea_2.wav delete mode 100644 frontends/sdl/audio/yea_3.wav create mode 100644 frontends/sdl/cetris_sdl.h create mode 100644 frontends/sdl/data/LICENSE.txt create mode 100644 frontends/sdl/data/OpenSans-Regular.ttf create mode 100644 frontends/sdl/data/yea_0.wav create mode 100644 frontends/sdl/data/yea_1.wav create mode 100644 frontends/sdl/data/yea_2.wav create mode 100644 frontends/sdl/data/yea_3.wav (limited to 'frontends') diff --git a/frontends/sdl/LICENSE.txt b/frontends/sdl/LICENSE.txt deleted file mode 100644 index 75b5248..0000000 --- a/frontends/sdl/LICENSE.txt +++ /dev/null @@ -1,202 +0,0 @@ - - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. diff --git a/frontends/sdl/OpenSans-Regular.ttf b/frontends/sdl/OpenSans-Regular.ttf deleted file mode 100644 index 29bfd35..0000000 Binary files a/frontends/sdl/OpenSans-Regular.ttf and /dev/null differ diff --git a/frontends/sdl/audio/yea_0.wav b/frontends/sdl/audio/yea_0.wav deleted file mode 100644 index 6fa9fdf..0000000 Binary files a/frontends/sdl/audio/yea_0.wav and /dev/null differ diff --git a/frontends/sdl/audio/yea_1.wav b/frontends/sdl/audio/yea_1.wav deleted file mode 100644 index 3c795ec..0000000 Binary files a/frontends/sdl/audio/yea_1.wav and /dev/null differ diff --git a/frontends/sdl/audio/yea_2.wav b/frontends/sdl/audio/yea_2.wav deleted file mode 100644 index 67511b9..0000000 Binary files a/frontends/sdl/audio/yea_2.wav and /dev/null differ diff --git a/frontends/sdl/audio/yea_3.wav b/frontends/sdl/audio/yea_3.wav deleted file mode 100644 index 4098797..0000000 Binary files a/frontends/sdl/audio/yea_3.wav and /dev/null differ diff --git a/frontends/sdl/cetris_sdl.h b/frontends/sdl/cetris_sdl.h new file mode 100644 index 0000000..8b41101 --- /dev/null +++ b/frontends/sdl/cetris_sdl.h @@ -0,0 +1,44 @@ +typedef struct { + SDL_Color main; + SDL_Color off; + SDL_Color text; +} color_scheme_t; + +color_scheme_t dark_mode = { + .main = (SDL_Color){100, 100, 100, 255}, + .off = (SDL_Color){50, 50, 50, 255}, + .text = (SDL_Color){240, 240, 240, 255} +}; + +color_scheme_t light_mode = { + .main = (SDL_Color){255, 255, 255, 255}, + .off = (SDL_Color){235, 235, 235, 255}, + .text = (SDL_Color){10, 10, 10, 255} +}; + +SDL_Color mino_colors[7] = { + (SDL_Color){253,253,150,255}, // Yellow + (SDL_Color){174,198,207,255}, // Aqua + (SDL_Color){255,105,97,255}, // Red + (SDL_Color){170,221,119,255}, // Olive + (SDL_Color){255,179,71,255}, // Orange + (SDL_Color){119,158,203,255}, // Navy + (SDL_Color){177,156,217,255} // Purple +}; + +typedef struct { + SDL_AudioSpec wav_spec; + uint32_t wav_length; + uint8_t *wav_buffer; +} audio_clip_t; + +typedef struct { + TTF_Font *font; + int size; +} font_t; + +typedef struct { + cetris_game* game; + color_scheme_t scheme; + float count_down; +} game_board_t; diff --git a/frontends/sdl/data/LICENSE.txt b/frontends/sdl/data/LICENSE.txt new file mode 100644 index 0000000..75b5248 --- /dev/null +++ b/frontends/sdl/data/LICENSE.txt @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/frontends/sdl/data/OpenSans-Regular.ttf b/frontends/sdl/data/OpenSans-Regular.ttf new file mode 100644 index 0000000..29bfd35 Binary files /dev/null and b/frontends/sdl/data/OpenSans-Regular.ttf differ diff --git a/frontends/sdl/data/yea_0.wav b/frontends/sdl/data/yea_0.wav new file mode 100644 index 0000000..6fa9fdf Binary files /dev/null and b/frontends/sdl/data/yea_0.wav differ diff --git a/frontends/sdl/data/yea_1.wav b/frontends/sdl/data/yea_1.wav new file mode 100644 index 0000000..3c795ec Binary files /dev/null and b/frontends/sdl/data/yea_1.wav differ diff --git a/frontends/sdl/data/yea_2.wav b/frontends/sdl/data/yea_2.wav new file mode 100644 index 0000000..67511b9 Binary files /dev/null and b/frontends/sdl/data/yea_2.wav differ diff --git a/frontends/sdl/data/yea_3.wav b/frontends/sdl/data/yea_3.wav new file mode 100644 index 0000000..4098797 Binary files /dev/null and b/frontends/sdl/data/yea_3.wav differ diff --git a/frontends/sdl/meson.build b/frontends/sdl/meson.build index 1953c08..e838ce9 100644 --- a/frontends/sdl/meson.build +++ b/frontends/sdl/meson.build @@ -14,13 +14,9 @@ else deps += dependency('SDL2_ttf') endif -configure_file(input: 'config.ini', - output: 'config.ini', - copy: true, install: false) - -configure_file(input: 'OpenSans-Regular.ttf', - output: 'OpenSans-Regular.ttf', - copy: true, install: false) +run_command( + 'bash', '-c', 'cp -r $MESON_SOURCE_ROOT/frontends/sdl/data $MESON_BUILD_ROOT/frontends/sdl' +) executable('cetris', src, dependencies: deps, diff --git a/frontends/sdl/sdl_ui.c b/frontends/sdl/sdl_ui.c index e5b371a..1af4f93 100644 --- a/frontends/sdl/sdl_ui.c +++ b/frontends/sdl/sdl_ui.c @@ -18,52 +18,27 @@ #include #include -#define W 570 -#define H 640 +#include "cetris_sdl.h" -#define BLOCK_SIZE 25 - -#define X_OFFSET (W/2) - (BLOCK_SIZE * 5) -#define Y_OFFSET (H/2) - (BLOCK_SIZE * 10) +#define W 1280 +#define H 720 +#define FRAME_RATE 60 SDL_Renderer* render; -TTF_Font* normal_font; -TTF_Font* big_font; SDL_Window *window; SDL_Surface *screen; -bool show_menu; -int menu_index; - -bool dark_mode; -SDL_Color main_color; -SDL_Color off; -SDL_Color text; - -float count_down; - cetris_game g; -typedef struct { - SDL_AudioSpec wav_spec; - uint32_t wav_length; - uint8_t *wav_buffer; -} audio_clip; +game_board_t main_board; -audio_clip clear_sound[4]; +int font_count; +font_t fonts[10]; + +audio_clip_t clear_sound[4]; SDL_AudioDeviceID audio_device; -uint8_t colors[7][3] = { - {253,253,150}, // Yellow - {174,198,207}, // Aqua - {255,105,97}, // Red - {170,221,119}, // Olive - {255,179,71}, // Orange - {119,158,203}, // Navy - {177,156,217} // Purple -}; - -void setup() { +void setup_sdl() { SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO); window = SDL_CreateWindow("cetris", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, W, H, SDL_WINDOW_SHOWN); screen = SDL_GetWindowSurface(window); @@ -73,27 +48,31 @@ void setup() { if (!render) exit(fprintf(stderr, "err: could not create SDL renderer\n")); TTF_Init(); - normal_font = TTF_OpenFont("OpenSans-Regular.ttf", 20); - big_font = TTF_OpenFont("OpenSans-Regular.ttf", 30); - TTF_SetFontHinting(normal_font, TTF_HINTING_MONO); - TTF_SetFontHinting(big_font, TTF_HINTING_MONO); for (int i = 0; i < 4; i++) { char name[25]; - sprintf(name, "audio/yea_%i.wav", i); + sprintf(name, "data/yea_%i.wav", i); SDL_LoadWAV(name, &(clear_sound[i].wav_spec), &(clear_sound[i].wav_buffer), &(clear_sound[i].wav_length)); } audio_device = SDL_OpenAudioDevice(NULL, 0, &(clear_sound[0].wav_spec), NULL, 0); if (audio_device == 0) printf("failed to open audio device\n"); } -void draw_text(char* string, int x, int y, bool big) { - SDL_Surface *surface; - if (big) { - surface = TTF_RenderText_Solid(big_font, string, text); - } else { - surface = TTF_RenderText_Solid(normal_font, string, text); +TTF_Font* get_font(int size) { + for (int i = 0; i < font_count; i++) { + if (fonts[i].size == size) { + return fonts[i].font; + } } + fonts[font_count].font = TTF_OpenFont("data/OpenSans-Regular.ttf", size); + TTF_SetFontHinting(fonts[font_count].font, TTF_HINTING_MONO); + fonts[font_count].size = size; + return fonts[font_count++].font; +} + +void draw_text(char* string, int x, int y, TTF_Font* font, SDL_Color color) { + SDL_Surface *surface; + surface = TTF_RenderText_Solid(font, string, color); SDL_Rect message; message.x = x; @@ -107,194 +86,171 @@ void draw_text(char* string, int x, int y, bool big) { SDL_FreeSurface(surface); } -void load_colors() { - if (dark_mode) { - main_color = (SDL_Color){100, 100, 100, 255}; - off = (SDL_Color){50, 50, 50, 255}; - text = (SDL_Color){240, 240, 240, 255}; - SDL_SetRenderDrawBlendMode(render, SDL_BLENDMODE_ADD); - } else { - main_color = (SDL_Color){255, 255, 255, 255}; - off = (SDL_Color){235, 235, 235, 255}; - text = (SDL_Color){10, 10, 10, 255}; - SDL_SetRenderDrawBlendMode(render, SDL_BLENDMODE_BLEND); - } +void draw_block(int x, int y, int width, int height, SDL_Color c, SDL_Color off) { + SDL_Rect b = {x, y, width, height}; + SDL_SetRenderDrawColor(render, c.r, c.g, c.b, c.a); + SDL_RenderFillRect(render, &b); + SDL_RenderDrawRect(render, &b); + SDL_SetRenderDrawColor(render, off.r, off.g, off.b, off.a); + b.y--; b.x--; b.w++; b.h++; + SDL_RenderDrawRect(render, &b); } -void draw_menu() { - SDL_Texture *m = SDL_CreateTexture(render, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, W, H); - SDL_SetRenderTarget(render, m); - - SDL_SetRenderDrawColor(render, main_color.r, main_color.g, main_color.b, 255); - SDL_RenderClear(render); - - if (menu_index == 0) { - draw_text("> Play <", 80, (H / 2) - 30, true); - } else { - draw_text("Play", 80, (H / 2) - 30, true); - } - if (menu_index == 1) { - draw_text("> Config <", 80, (H / 2), true); - } else { - draw_text("Config", 80, (H / 2), true); - } +void draw_board(game_board_t* board, int x, int y, int w, int h) { + SDL_Rect background = {x, y, w, h}; - SDL_SetRenderTarget(render, NULL); - - SDL_Point center = {W / 2, H / 2}; - SDL_RenderCopyEx(render, m, NULL, NULL, 0, ¢er, SDL_FLIP_NONE); + SDL_SetRenderDrawColor(render, + board->scheme.off.r - (fmod((double)board->count_down, (double)1.0) * 50), + board->scheme.off.g, board->scheme.off.b, board->scheme.off.a); - SDL_DestroyTexture(m); -} + SDL_RenderFillRect(render, &background); + SDL_RenderDrawRect(render, &background); -void draw_game() { - SDL_Texture *m = SDL_CreateTexture(render, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, W, H); - SDL_SetRenderTarget(render, m); - - SDL_SetRenderDrawColor(render, main_color.r, main_color.g, main_color.b, 255); - SDL_RenderClear(render); + SDL_SetRenderDrawColor(render, board->scheme.main.r, + board->scheme.main.g, board->scheme.main.b, board->scheme.main.a); - SDL_Rect board = {X_OFFSET + 1, Y_OFFSET, 250, 500}; - SDL_Rect queue = {W - (W / 4), Y_OFFSET, (BLOCK_SIZE * 4) + 20, BLOCK_SIZE * 5 * 3}; - SDL_Rect hold = {30, Y_OFFSET, BLOCK_SIZE * 4, BLOCK_SIZE * 4}; - SDL_SetRenderDrawColor(render, off.r - (fmod((double)count_down, (double)1.0) * 50), off.g, off.b, 255); - SDL_RenderFillRect(render, &board); - SDL_RenderFillRect(render, &queue); - SDL_RenderFillRect(render, &hold); - SDL_RenderDrawRect(render, &board); - SDL_RenderDrawRect(render, &queue); - SDL_RenderDrawRect(render, &hold); + int board_x = board->game->config.board_x; + int board_y = board->game->config.board_y; + int board_visible = board_y - board->game->config.board_visible; - int BOARD_Y = g.config.board_y; - int BOARD_X = g.config.board_x; - int BOARD_VISIBLE = g.config.board_y - g.config.board_visible; + float block_width = w / board->game->config.board_x; + float block_height = h / board->game->config.board_visible; - SDL_SetRenderDrawColor(render, main_color.r, main_color.g, main_color.b, 255); - for (int x = 0; x < BOARD_X + 1; x++) { - int rx = X_OFFSET + 1 + (x * BLOCK_SIZE); - SDL_RenderDrawLine(render, rx, Y_OFFSET + 1, rx, Y_OFFSET + 500); + for (int s = 0; s < board_x + 1; s++) { + int rx = x + (s * block_width); + SDL_RenderDrawLine(render, rx, y + 1, rx, y + h); } - for (int y = 0; y < BOARD_Y - BOARD_VISIBLE + 1; y++) { - int ry = Y_OFFSET + (y * BLOCK_SIZE); - SDL_RenderDrawLine(render, X_OFFSET + 1, ry, X_OFFSET + 250, ry); + + for (int j = 0; j < board->game->config.board_visible + 1; j++) { + int ry = y + (j * block_height); + SDL_RenderDrawLine(render, x, ry, x + w, ry); } - SDL_Rect b = {0, 0, BLOCK_SIZE, BLOCK_SIZE}; - SDL_Rect w = {0, 0, BLOCK_SIZE + 1, BLOCK_SIZE + 1}; + for (int s = board->game->highest_line; s < board_y; s++) { + for (int j = 0; j < board_x; j++) { + if (board->game->board[j][s] & SLOT_OCCUPIED) { + if (g.line_remove_tick[s]) { + if (board->game->tick % 2 == 0) { + continue; + } + } - for (int y = 0; y < 4; y++) { - for (int x = 0; x < 4; x++) { - if ((g.current.m[y]>>(3 - x))&1) { - b.x = X_OFFSET + ((x + g.current.pos.x) * BLOCK_SIZE) + 1; - b.y = (Y_OFFSET + ((y + g.current.pos.y) * BLOCK_SIZE)) - (BOARD_VISIBLE * BLOCK_SIZE); - - uint8_t (*color)[3] = &(colors[g.current.t]); - SDL_SetRenderDrawColor(render, (*color)[0], (*color)[1], (*color)[2], 255); - - SDL_RenderFillRect(render, &b); - SDL_RenderDrawRect(render, &b); + int block_x = x + (j * block_width); + int block_y = (y + ((s - board_visible) * block_height)); + SDL_Color mino_color = mino_colors[(board->game->board[j][s] >> 5)]; - SDL_SetRenderDrawColor(render, off.r, off.g, off.b, 255); - w.y = b.y - 1; - w.x = b.x - 1; - SDL_RenderDrawRect(render, &w); + draw_block(block_x, block_y, block_width, block_height, + mino_color, board->scheme.off); + } + } + } - b.y = (Y_OFFSET + ((y + g.current.ghost_y) * BLOCK_SIZE)) - (BOARD_VISIBLE * BLOCK_SIZE); + for (int s = 0; s < 4; s++) { + for (int j = 0; j < 4; j++) { + if ((board->game->current.m[s]>>(3 - j))&1) { + int block_x = x + ((j + board->game->current.pos.x) * block_width); + int block_y = y + ((s + board->game->current.pos.y) - board_visible) * block_height; + + SDL_Color mino_color = mino_colors[board->game->current.t]; + draw_block(block_x, block_y, block_width, block_height, + mino_color, board->scheme.off); + + int ghost_y = y + ((s + board->game->current.ghost_y) - board_visible) * block_height; + if (ghost_y != block_y) { + mino_color.a -= 100; + + draw_block(block_x, ghost_y, block_width, block_height, + mino_color, board->scheme.off); + } + } + } + } - w.y = b.y - 1; - SDL_RenderDrawRect(render, &w); - - SDL_SetRenderDrawColor(render, (*color)[0], (*color)[1], (*color)[2], 160); - SDL_RenderFillRect(render, &b); - SDL_RenderDrawRect(render, &b); + if (board->count_down > 0) board->count_down-=(1.0/FRAME_RATE); +} - } +void draw_held_piece(game_board_t* board, int x, int y, int w, int h) { + float block_width = w / 4; + float block_height = h / 4; - if (g.piece_held) { - if ((g.held.m[y]>>(3 - x))&1) { - b.x = 40 + ((x) * BLOCK_SIZE); - b.y = Y_OFFSET + ((y) * BLOCK_SIZE); - if (g.held.t == MINO_I) { - b.x -= 10; - b.y += 10; - } - if (g.held.t == MINO_O) b.x -= 10; + SDL_Rect hold = {x, y, w + 1, h}; - uint8_t (*color)[3] = &(colors[g.held.t]); - SDL_SetRenderDrawColor(render, (*color)[0], (*color)[1], (*color)[2], 255); - - SDL_RenderFillRect(render, &b); - SDL_RenderDrawRect(render, &b); + SDL_SetRenderDrawColor(render, board->scheme.main.r, + board->scheme.main.g, board->scheme.main.b, board->scheme.main.a); - SDL_SetRenderDrawColor(render, off.r, off.g, off.b, 255); - w.y = b.y - 1; - w.x = b.x - 1; - SDL_RenderDrawRect(render, &w); + SDL_RenderFillRect(render, &hold); + SDL_RenderDrawRect(render, &hold); + + if (!board->game->piece_held) return; + for (int s = 0; s < 4; s++) { + for (int j = 0; j < 4; j++) { + if ((board->game->held.m[s]>>(3 - j))&1) { + float block_x = x + (j * block_width) + (block_width / 2.0); + int block_y = y + (s * block_height); + if (board->game->held.t == MINO_I) { + block_x -= block_width / 2; + block_x++; + block_y += block_height / 2; } + if (board->game->held.t == MINO_O) + block_x -= block_width / 2; + + SDL_Color mino_color = mino_colors[board->game->held.t]; + draw_block(block_x, block_y, block_width, block_height, + mino_color, board->scheme.off); + } + } + } +} - for (int i = 0; i < 5; i++) { - int index = (g.current_index + i); +void draw_piece_queue(game_board_t* board, int x, int y, int w, int h) { + float block_width = (w / 4); + float block_height = h / 15; - uint8_t mino; - if (index <= 6) { - mino = g.piece_queue[index]; - } else { - index = index % 7; - mino = g.next_queue[index]; - } + SDL_SetRenderDrawColor(render, board->scheme.main.r, + board->scheme.main.g, board->scheme.main.b, board->scheme.main.a); - uint8_t (*color)[3] = &(colors[mino]); - uint8_t res = (default_matrices[mino][y]>>(3 - x))&1; + SDL_Rect queue = {x, y, w + 1, h}; + SDL_RenderFillRect(render, &queue); + SDL_RenderDrawRect(render, &queue); - if (res) { - b.x = 20 + (W - (W / 4)) + (x * BLOCK_SIZE); - b.y = (Y_OFFSET - 10) + (BLOCK_SIZE * i * 3) + (y * BLOCK_SIZE); - if (mino == MINO_I) { - b.x -= 10; - b.y += 10; - } - if (mino == MINO_O) b.x -= 10; - SDL_SetRenderDrawColor(render, (*color)[0], (*color)[1], (*color)[2], 255); - - SDL_RenderFillRect(render, &b); - SDL_RenderDrawRect(render, &b); + for (int i = 0; i < 5; i++) { + int index = (board->game->current_index + i); - SDL_SetRenderDrawColor(render, off.r, off.g, off.b, 255); - w.y = b.y - 1; - w.x = b.x - 1; - SDL_RenderDrawRect(render, &w); - } - } + uint8_t mino; + if (index <= 6) { + mino = board->game->piece_queue[index]; + } else { + index = index % 7; + mino = board->game->next_queue[index]; } - } - for (int x = 0; x < BOARD_X; x++) { - for (int y = g.highest_line; y < BOARD_Y; y++) { - if (g.board[x][y] & SLOT_OCCUPIED) { - b.x = X_OFFSET + (x * BLOCK_SIZE) + 1; - b.y = (Y_OFFSET + (y * BLOCK_SIZE)) - (BOARD_VISIBLE * BLOCK_SIZE); - - if (g.line_remove_tick[y]) { - if ((int)g.tick % 2 == 0) { - continue; + for (int s = 0; s < 4; s++) { + for (int j = 0; j < 4; j++) { + + if ((default_matrices[mino][s]>>(3 - j))&1) { + float block_x = x + (j) * block_width + (block_width / 2.0); + int block_y = y + ((i * 3) + s) * block_height; + if (mino == MINO_I) { + block_x -= block_width / 2; + block_x++; + block_y += block_height / 2; } - } - - SDL_SetRenderDrawColor(render, off.r, off.g, off.b, 255); - w.y = b.y - 1; - w.x = b.x - 1; - SDL_RenderDrawRect(render, &w); + if (mino == MINO_O) + block_x -= block_width / 2; - uint8_t (*color)[3] = &(colors[(g.board[x][y] >> 5)]); - SDL_SetRenderDrawColor(render, (*color)[0], (*color)[1], (*color)[2], 255); - SDL_RenderFillRect(render, &b); + SDL_Color mino_color = mino_colors[mino]; + draw_block(block_x, block_y, block_width, block_height, + mino_color, board->scheme.off); - SDL_RenderDrawRect(render, &b); + } } } } +} +void draw_timer(game_board_t *board, int x, int y) { char *buf = malloc(200); long double second = g.timer / 1000000.0f; if (second > 60.0f) { @@ -304,72 +260,90 @@ void draw_game() { } else { sprintf(buf, "%.6Lf", second); } - draw_text(buf, 20, H - 40, false); + draw_text(buf, x, y, get_font(25), board->scheme.text); - sprintf(buf, "lines remaining: %i", 20 - g.lines); - draw_text(buf, 20, H - 60, false); + //sprintf(buf, "lines remaining: %i", 20 - g.lines); + //draw_text(buf, 20, H - 60, false); free(buf); +} + + +void draw() { + SDL_Texture *m = SDL_CreateTexture(render, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, W, H); + SDL_SetRenderTarget(render, m); + + SDL_SetRenderDrawColor(render, main_board.scheme.main.r, + main_board.scheme.main.g, main_board.scheme.main.b, 255); + SDL_RenderClear(render); + + draw_board(&main_board, (W / 2) - 125, (H / 2) - 250, 250, 500); + draw_held_piece(&main_board, (W / 2) - 225, (H / 2) - 250, 100, 100); + draw_piece_queue(&main_board, (W / 2) + 125, (H / 2) - 250, 100, 400); + draw_timer(&main_board, 20, 20); SDL_SetRenderTarget(render, NULL); SDL_Point center = {W / 2, H / 2}; SDL_RenderCopyEx(render, m, NULL, NULL, 0, ¢er, SDL_FLIP_NONE); + SDL_RenderPresent(render); SDL_DestroyTexture(m); - - if (count_down > 0) { - count_down-=(1.0/60.0); - } } int main(void) { - setup(); - - ini_parser p; - load_ini_file(&p, "config.ini"); + setup_sdl(); cetris_config config = tetris_ds_config; - - int das = atoi(get_ini_value(&p, "das", "das")); - config.das_das = das; - - int arr = atoi(get_ini_value(&p, "das", "arr")); - config.das_arr = arr; - config.win_condition = twenty_line_sprint; config.levels = &tetris_worlds_levels[0]; config.wait_on_clear = 0; - char* drop_delay = get_ini_value(&p, "game", "drop_delay"); - if (drop_delay) config.drop_period = atoi(drop_delay); - - char* next_piece_delay = get_ini_value(&p, "game", "next_piece_delay"); - if (next_piece_delay) config.next_piece_delay = atoi(next_piece_delay); + bool dark_theme = false; + + ini_parser p; + if (load_ini_file(&p, "config.ini")) { + int das = atoi(get_ini_value(&p, "das", "das")); + config.das_das = das; - char* lock_delay = get_ini_value(&p, "game", "lock_delay"); - if (lock_delay) config.lock_delay = atoi(lock_delay); + int arr = atoi(get_ini_value(&p, "das", "arr")); + config.das_arr = arr; - char* force_lock = get_ini_value(&p, "game", "force_lock"); - if (force_lock) config.force_lock = atoi(force_lock); + char* drop_delay = get_ini_value(&p, "game", "drop_delay"); + if (drop_delay) config.drop_period = atoi(drop_delay); + + char* next_piece_delay = get_ini_value(&p, "game", "next_piece_delay"); + if (next_piece_delay) config.next_piece_delay = atoi(next_piece_delay); - char* wait_on_clear = get_ini_value(&p, "game", "wait_on_clear"); - if (wait_on_clear) config.wait_on_clear = atoi(wait_on_clear); + char* lock_delay = get_ini_value(&p, "game", "lock_delay"); + if (lock_delay) config.lock_delay = atoi(lock_delay); - char* line_delay_clear = get_ini_value(&p, "game", "line_clear_delay"); - if (line_delay_clear) config.line_delay_clear = atoi(line_delay_clear); + char* force_lock = get_ini_value(&p, "game", "force_lock"); + if (force_lock) config.force_lock = atoi(force_lock); - char *dark = get_ini_value(&p, "ui", "dark_mode"); - if (dark) dark_mode = atoi(dark); - else dark_mode = 0; + char* wait_on_clear = get_ini_value(&p, "game", "wait_on_clear"); + if (wait_on_clear) config.wait_on_clear = atoi(wait_on_clear); - load_colors(); + char* line_delay_clear = get_ini_value(&p, "game", "line_clear_delay"); + if (line_delay_clear) config.line_delay_clear = atoi(line_delay_clear); + + char *dark = get_ini_value(&p, "ui", "dark_mode"); + if (dark && atoi(dark)) { + dark_theme = true; + } + } - show_menu = false; - menu_index = 0; + if (dark_theme) { + main_board.scheme = dark_mode; + SDL_SetRenderDrawBlendMode(render, SDL_BLENDMODE_ADD); + } else { + main_board.scheme = light_mode; + SDL_SetRenderDrawBlendMode(render, SDL_BLENDMODE_BLEND); + } - count_down = 3; + main_board.count_down = 3; init_game(&g, &config); + main_board.game = &g; SDL_Event e; for(;;) { @@ -412,7 +386,7 @@ int main(void) { move_piece(&g, ROTATE_CCW); break; case 'r': cetris_stop_game(&g); - count_down = 3; + main_board.count_down = 3; break; } break; @@ -437,16 +411,11 @@ int main(void) { } } - if (show_menu) { - draw_menu(); - } else { - draw_game(); - } - - if (count_down < 0 && g.waiting) { + draw(); + + if (main_board.count_down < 0 && g.waiting) { cetris_start_game(&g); } - SDL_RenderPresent(render); if (g.line_event) { int index = g.line_combo - 1; @@ -456,7 +425,7 @@ int main(void) { g.line_event = false; } - SDL_Delay(1000 / 60); + SDL_Delay(1000 / FRAME_RATE); } } -- cgit v1.2.3-101-g0448