├── .gitignore ├── CMakeLists.txt ├── IDE_USAGE.md ├── LICENSE ├── README.md ├── cmake └── sdl2 │ ├── Copyright.txt │ ├── FindSDL2.cmake │ ├── FindSDL2_gfx.cmake │ ├── FindSDL2_image.cmake │ ├── FindSDL2_mixer.cmake │ ├── FindSDL2_net.cmake │ ├── FindSDL2_ttf.cmake │ └── README.md ├── images ├── animation.png ├── blue-line.png ├── calculator.png ├── display-text.png ├── hello-world.png ├── mouse-hover.png ├── paint.png ├── red-dot.png └── text-editor.png ├── include └── .include_dir ├── rename_project.sh └── src └── main.c /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | build-*/ 3 | CMakeLists.txt.* 4 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright (c) 2018, 2019 Amine Ben Hassouna 3 | # 4 | # Permission is hereby granted, free of charge, to any 5 | # person obtaining a copy of this software and associated 6 | # documentation files (the "Software"), to deal in the 7 | # Software without restriction, including without 8 | # limitation the rights to use, copy, modify, merge, 9 | # publish, distribute, sublicense, and/or sell copies of 10 | # the Software, and to permit persons to whom the Software 11 | # is furnished to do so, subject to the following 12 | # conditions: 13 | # 14 | # The above copyright notice and this permission notice 15 | # shall be included in all copies or substantial portions 16 | # of the Software. 17 | # 18 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 19 | # ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 20 | # TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 21 | # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 22 | # SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 23 | # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 24 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 25 | # IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | # DEALINGS IN THE SOFTWARE. 27 | # 28 | 29 | # Define the project name 30 | project(sdl2-pointerless) 31 | 32 | # Set the cmake minimum version to 3.5.1 33 | cmake_minimum_required(VERSION 3.5.1) 34 | 35 | # Add SDL2 CMake modules 36 | list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/sdl2) 37 | 38 | # Add all c source files under the src directory 39 | file(GLOB SOURCES "src/*.c") 40 | add_executable(${PROJECT_NAME} ${SOURCES}) 41 | 42 | # Add all headers files under the include directory 43 | target_include_directories(${PROJECT_NAME} PRIVATE include) 44 | 45 | # Add compiler errors/warnings flags 46 | target_compile_options(${PROJECT_NAME} PRIVATE $<$:/W4 /WX>) 47 | target_compile_options(${PROJECT_NAME} PRIVATE $<$>:-Wall -Wextra -pedantic>) 48 | 49 | 50 | # Add SDL2 library 51 | find_package(SDL2 REQUIRED) 52 | target_link_libraries(${PROJECT_NAME} SDL2::Main) 53 | 54 | # Add SDL2_image library 55 | #find_package(SDL2_image REQUIRED) 56 | #target_link_libraries(${PROJECT_NAME} SDL2::Image) 57 | 58 | # Add SDL2_ttf library 59 | #find_package(SDL2_ttf REQUIRED) 60 | #target_link_libraries(${PROJECT_NAME} SDL2::TTF) 61 | 62 | # Add SDL2_net library 63 | #find_package(SDL2_net REQUIRED) 64 | #target_link_libraries(${PROJECT_NAME} SDL2::Net) 65 | 66 | # Add SDL2_mixer library 67 | #find_package(SDL2_mixer REQUIRED) 68 | #target_link_libraries(${PROJECT_NAME} SDL2::Mixer) 69 | 70 | # Add SDL2_gfx library 71 | #find_package(SDL2_gfx REQUIRED) 72 | #target_link_libraries(${PROJECT_NAME} SDL2::GFX) 73 | 74 | -------------------------------------------------------------------------------- /IDE_USAGE.md: -------------------------------------------------------------------------------- 1 | # CMake sdl project - IDE usage 2 | 3 | There is many IDEs that support CMake projects. It could be natively or via plugins.
4 | This is a short tutorial on how we can use some IDEs to open a CMake project on linux: 5 | 6 | - [Qt Creator](#open-the-project-with-qt-creator) 7 | - [Code::Blocks](#open-the-project-with-codeblocks) 8 | - [Eclipse](#open-the-project-with-eclipse-cc) 9 | 10 | ## Open the project with Qt Creator 11 | 12 | [Qt Creator][] is a cross-platform C/C++ IDE, originally dedicated for the Qt framework. 13 | It handle natively CMake projects and provide an efficient code completion. 14 | 15 | *Install Qt creator:* 16 | 17 | Install from Debian/Ubuntu repo: 18 | 19 | ```sh 20 | sudo apt install qtcreator 21 | ``` 22 | 23 | Online installer: https://www.qt.io/download-thank-you?hsLang=en
24 | Offline installer: https://www.qt.io/offline-installers 25 | 26 | *Open the project:*
27 | Run Qt Creator, use `Open Files or Project ...` and select the `CMakeLists.txt` 28 | file of the cloned project.
29 | Finally, build and run the project. 30 | 31 | 32 | ## Open the project with Code::blocks 33 | 34 | [Code::Blocks][] is a well-known cross-platform C/C++ and Fortran IDE. 35 | It handle CMake projects by wrapping them into native Code::Blocks projects. 36 | 37 | *Install Code::Blocks* 38 | 39 | ```sh 40 | sudo apt install codeblocks 41 | ``` 42 | 43 | *Prepare the Code::Blocks project:* 44 | 45 | ```sh 46 | # Create a folder for the Code::Blocks project 47 | cd sdl2-pointerless 48 | mkdir -p build/codeblocks 49 | cd build/codeblocks 50 | 51 | # Generate a Code::Blocks project 52 | cmake ../.. -G "CodeBlocks - Unix Makefiles" 53 | ``` 54 | 55 | *Open the project:*
56 | Run Code::Blocks, and open the project in `sdl2-pointerless/build/codeblocks`.
57 | Finally, build and run the project. 58 | 59 | 60 | ## Open the project with Eclipse C/C++ 61 | 62 | [Eclipse][] is a well-known IDE, widely used in Java projects development, but 63 | it also supports other programming languages, like in this case C/C++. 64 | It handle CMake projects by wrapping them into native Eclipse projects. 65 | 66 | *Download Eclipse C/C++:* 67 | 68 | Online installer: https://www.eclipse.org/downloads
69 | Offline package: https://www.eclipse.org/downloads/packages 70 | 71 | *Prepare the Eclipse project:* 72 | 73 | ```sh 74 | # Create a folder for the Eclipse project outside this project 75 | cd sdl2-pointerless 76 | mkdir ../eclipse-sdl2-pointerless 77 | cd ../eclipse-sdl2-pointerless 78 | 79 | # Generate an Eclipse project 80 | cmake ../sdl2-pointerless -G "Eclipse CDT4 - Unix Makefiles" 81 | ``` 82 | *Open the project:*
83 | Run Eclipse, and open the project in `eclipse-sdl2-pointerless`.
84 | Create a new run configuration: Go to `Run` > `Run configurations` > 85 | `C\C++ Application` and specify the C/C++ Application using `Search Project...`
86 | Finally, build and run the project. 87 | 88 | 89 | 90 | [Qt Creator]: https://doc.qt.io/qtcreator 91 | [Code::Blocks]: http://www.codeblocks.org 92 | [Eclipse]: https://www.eclipse.org 93 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | # MIT license 2 | 3 | Copyright (c) 2018, 2019 Amine Ben Hassouna 4 | All rights reserved. 5 | 6 | Permission is hereby granted, free of charge, to any 7 | person obtaining a copy of this software and associated 8 | documentation files (the "Software"), to deal in the 9 | Software without restriction, including without 10 | limitation the rights to use, copy, modify, merge, 11 | publish, distribute, sublicense, and/or sell copies of 12 | the Software, and to permit persons to whom the Software 13 | is furnished to do so, subject to the following 14 | conditions: 15 | 16 | The above copyright notice and this permission notice 17 | shall be included in all copies or substantial portions 18 | of the Software. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 21 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 22 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 23 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 24 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 25 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 26 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 27 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 28 | DEALINGS IN THE SOFTWARE. 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SDL2 pointerless basic project 2 | 3 | This is a **one-file** C [SDL2][SDL] project. It is meant for beginners to let 4 | them create SDL-based projects **without** any *pointer* specific knowledge. 5 | It offers a micro-library that help create any grid-based user interface. 6 | The only dependency of this project is the SDL2 library. 7 | 8 | For those who can deal with pointers and multi-file project with CMake, it is 9 | recommended to use the [Basic C SDL2 game](https://github.com/aminosbh/basic-c-sdl-game) 10 | as a base for any grid-based project/game and the 11 | [Flying plane SDL2 animation](https://github.com/aminosbh/flying-plane-sdl-animation) 12 | for more sophisticated projects/games. 13 | 14 | A list of examples, projects and tutorials could be found at 15 | [SDL/SDL2 samples and projects](https://github.com/aminosbh/sdl2-samples-and-projects) 16 | 17 | ## Getting started 18 | 19 | You can use any IDE with C/C++ and SDL2 support like Code::Blocks. 20 | You can follow the following tutorial: [How to setup SDL2 with Code::Blocks on Windows](https://github.com/aminosbh/sdl2-pointerless/wiki/How-to-setup-SDL2-with-Code::Blocks-on-Windows) 21 | Just copy all the content of `src/main.c` and put it in place of any code in 22 | your default `main.c` or `main.cpp`. 23 | Try to execute the code as it is to check that it is running successfully. 24 | 25 | You can now start editing the `draw` function (at line #1725). 26 | The micro-library offers some basic functions to start coding the UI: 27 | 28 | ``` 29 | set_background_color(color) 30 | Set the background color to one of the predefined colors 31 | 32 | set_cell_color(x, y, color) 33 | Set the color of the cell at coordinates (x, y) 34 | 35 | set_grid_border_color(color) 36 | Set the grid (and cells) border color 37 | 38 | delay(ms) 39 | Wait a specified number of milliseconds 40 | 41 | get_key() 42 | Get the pressed key 43 | 44 | draw_char(c, at_x, at_y, color) 45 | Write a character at position (at_x, at_y) with a specific color 46 | 47 | draw_text(text, at_x, at_y, space, color) 48 | Write a text at position (at_x, at_y) with a specific color 49 | The space parameter represents the spacing between characters 50 | ``` 51 | 52 | The dimensions and the colors of the screen and the grid could be modified. 53 | You have just to update the values of the following (`main.c`, line 2): 54 | 55 | ``` 56 | // Define screen dimensions 57 | #define SCREEN_WIDTH (800) 58 | #define SCREEN_HEIGHT (600) 59 | 60 | // Max grid dimension 61 | #define GRID_MAX_X_CELLS (10) 62 | #define GRID_MAX_Y_CELLS (10) 63 | 64 | #define GRID_DEFAULT_MARGIN (20) 65 | #define GRID_DEFAULT_COLOR COLOR_WHITE 66 | #define GRID_DEFAULT_BORDER_SIZE (2) 67 | #define GRID_DEFAULT_BORDER_COLOR COLOR_GRAY 68 | ``` 69 | 70 | *The full list of features can be found in the `src/main.c` documentation section.* 71 | 72 | ## Examples 73 | 74 | **Draw a red dot at coordinates x = 1, y = 2 :** 75 | 76 | ```c 77 | void draw(int x_cells, int y_cells) 78 | { 79 | set_cell_color(1, 2, COLOR_RED); 80 | } 81 | ``` 82 | 83 | ![Red dot](/images/red-dot.png) 84 | 85 | 86 | **Draw a horizontal blue line:** 87 | 88 | ```c 89 | void draw(int x_cells, int y_cells) 90 | { 91 | for(int x = 0; x < x_cells; x++) 92 | { 93 | set_cell_color(x, 1, COLOR_BLUE); 94 | } 95 | } 96 | ``` 97 | 98 | ![Horizontal blue line](/images/blue-line.png) 99 | 100 | 101 | **Draw a horizontal blue line animated:** 102 | 103 | ```c 104 | void draw(int x_cells, int y_cells) 105 | { 106 | for(int x = 0; x < x_cells; x++) 107 | { 108 | set_cell_color(x, 1, COLOR_BLUE); 109 | delay(100); 110 | } 111 | } 112 | ``` 113 | 114 | **Implement a simple/interactive animation:** 115 | 116 | See the branch [example/animation](https://github.com/aminosbh/sdl2-pointerless/blob/example/animation/src/main.c#L1725). 117 | 118 | ![Animation](/images/animation.png) 119 | 120 | 121 | **Display text:** 122 | 123 | ```c 124 | void draw(int x_cells, int y_cells) 125 | { 126 | draw_text("Hello world", 1, 1, 1, COLOR_RED); 127 | } 128 | ``` 129 | 130 | ![Hello World](/images/hello-world.png) 131 | 132 | 133 | See the branch [example/display-text](https://github.com/aminosbh/sdl2-pointerless/blob/example/display-text/src/main.c#L1725). 134 | 135 | ![Display text](/images/display-text.png) 136 | 137 | 138 | **Implement a calculator:** 139 | 140 | See the branch [example/calculator](https://github.com/aminosbh/sdl2-pointerless/blob/example/calculator/src/main.c#L1725). 141 | 142 | ![Calculator](/images/calculator.png) 143 | 144 | 145 | **Implement a simple text editor:** 146 | 147 | See the branch [example/text-editor](https://github.com/aminosbh/sdl2-pointerless/blob/example/text-editor/src/main.c#L1725). 148 | 149 | ![Text editor](/images/text-editor.png) 150 | 151 | 152 | **Implement a simple painting app:** 153 | 154 | See the branch [example/paint](https://github.com/aminosbh/sdl2-pointerless/blob/example/paint/src/main.c#L1725). 155 | 156 | ![Text editor](/images/paint.png) 157 | 158 | **Implement mouse hover:** 159 | 160 | See the branch [example/mouse-hover](https://github.com/aminosbh/sdl2-pointerless/blob/example/mouse-hover/src/main.c#L1725). 161 | 162 | ![Text editor](/images/mouse-hover.png) 163 | 164 | 165 | ## Build on linux 166 | 167 | ### Dependencies 168 | 169 | - [Git][] 170 | - C Compiler (gcc, ...) 171 | - [CMake][] 172 | - [SDL2][SDL] library 173 | 174 | **On Debian/Ubuntu based distributions, use the following command:** 175 | 176 | ```sh 177 | sudo apt install git build-essential pkg-config cmake cmake-data libsdl2-dev 178 | ``` 179 | 180 | ### Build instructions 181 | 182 | ```sh 183 | # Clone this repo 184 | git clone https://gitlab.com/aminosbh/sdl2-pointerless.git 185 | cd sdl2-pointerless 186 | 187 | # Create a build folder 188 | mkdir build 189 | cd build 190 | 191 | # Build 192 | cmake .. 193 | make 194 | 195 | # Run 196 | ./sdl2-pointerless 197 | ``` 198 | 199 | ### Open the project with an IDE under Linux 200 | 201 | See [IDE_USAGE.md](IDE_USAGE.md) for details. 202 | 203 | ## License 204 | 205 | Author: Amine B. Hassouna [@aminosbh](https://github.com/aminosbh) 206 | 207 | This project is distributed under the terms of the MIT license 208 | [<LICENSE>](LICENSE). 209 | 210 | 211 | 212 | [SDL]: https://www.libsdl.org 213 | [CMake]: https://cmake.org 214 | [Git]: https://git-scm.com 215 | [SDL2_image]: https://www.libsdl.org/projects/SDL_image 216 | [SDL2_ttf]: https://www.libsdl.org/projects/SDL_ttf 217 | [SDL2_net]: https://www.libsdl.org/projects/SDL_net 218 | [SDL2_mixer]: https://www.libsdl.org/projects/SDL_mixer 219 | [SDL2_gfx]: http://www.ferzkopp.net/wordpress/2016/01/02/sdl_gfx-sdl2_gfx 220 | -------------------------------------------------------------------------------- /cmake/sdl2/Copyright.txt: -------------------------------------------------------------------------------- 1 | CMake - Cross Platform Makefile Generator 2 | Copyright 2000-2019 Kitware, Inc. and Contributors 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions 7 | are met: 8 | 9 | * Redistributions of source code must retain the above copyright 10 | notice, this list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright 13 | notice, this list of conditions and the following disclaimer in the 14 | documentation and/or other materials provided with the distribution. 15 | 16 | * Neither the name of Kitware, Inc. nor the names of Contributors 17 | may be used to endorse or promote products derived from this 18 | software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | 32 | ------------------------------------------------------------------------------ 33 | 34 | The following individuals and institutions are among the Contributors: 35 | 36 | * Aaron C. Meadows 37 | * Adriaan de Groot 38 | * Aleksey Avdeev 39 | * Alexander Neundorf 40 | * Alexander Smorkalov 41 | * Alexey Sokolov 42 | * Alex Merry 43 | * Alex Turbov 44 | * Amine Ben Hassouna 45 | * Andreas Pakulat 46 | * Andreas Schneider 47 | * André Rigland Brodtkorb 48 | * Axel Huebl, Helmholtz-Zentrum Dresden - Rossendorf 49 | * Benjamin Eikel 50 | * Bjoern Ricks 51 | * Brad Hards 52 | * Christopher Harvey 53 | * Christoph Grüninger 54 | * Clement Creusot 55 | * Daniel Blezek 56 | * Daniel Pfeifer 57 | * Enrico Scholz 58 | * Eran Ifrah 59 | * Esben Mose Hansen, Ange Optimization ApS 60 | * Geoffrey Viola 61 | * Google Inc 62 | * Gregor Jasny 63 | * Helio Chissini de Castro 64 | * Ilya Lavrenov 65 | * Insight Software Consortium 66 | * Jan Woetzel 67 | * Julien Schueller 68 | * Kelly Thompson 69 | * Laurent Montel 70 | * Konstantin Podsvirov 71 | * Mario Bensi 72 | * Martin Gräßlin 73 | * Mathieu Malaterre 74 | * Matthaeus G. Chajdas 75 | * Matthias Kretz 76 | * Matthias Maennich 77 | * Michael Hirsch, Ph.D. 78 | * Michael Stürmer 79 | * Miguel A. Figueroa-Villanueva 80 | * Mike Jackson 81 | * Mike McQuaid 82 | * Nicolas Bock 83 | * Nicolas Despres 84 | * Nikita Krupen'ko 85 | * NVIDIA Corporation 86 | * OpenGamma Ltd. 87 | * Patrick Stotko 88 | * Per Øyvind Karlsen 89 | * Peter Collingbourne 90 | * Petr Gotthard 91 | * Philip Lowman 92 | * Philippe Proulx 93 | * Raffi Enficiaud, Max Planck Society 94 | * Raumfeld 95 | * Roger Leigh 96 | * Rolf Eike Beer 97 | * Roman Donchenko 98 | * Roman Kharitonov 99 | * Ruslan Baratov 100 | * Sebastian Holtermann 101 | * Stephen Kelly 102 | * Sylvain Joubert 103 | * Thomas Sondergaard 104 | * Tobias Hunger 105 | * Todd Gamblin 106 | * Tristan Carel 107 | * University of Dundee 108 | * Vadim Zhukov 109 | * Will Dicharry 110 | 111 | See version control history for details of individual contributions. 112 | 113 | The above copyright and license notice applies to distributions of 114 | CMake in source and binary form. Third-party software packages supplied 115 | with CMake under compatible licenses provide their own copyright notices 116 | documented in corresponding subdirectories or source files. 117 | 118 | ------------------------------------------------------------------------------ 119 | 120 | CMake was initially developed by Kitware with the following sponsorship: 121 | 122 | * National Library of Medicine at the National Institutes of Health 123 | as part of the Insight Segmentation and Registration Toolkit (ITK). 124 | 125 | * US National Labs (Los Alamos, Livermore, Sandia) ASC Parallel 126 | Visualization Initiative. 127 | 128 | * National Alliance for Medical Image Computing (NAMIC) is funded by the 129 | National Institutes of Health through the NIH Roadmap for Medical Research, 130 | Grant U54 EB005149. 131 | 132 | * Kitware, Inc. 133 | -------------------------------------------------------------------------------- /cmake/sdl2/FindSDL2.cmake: -------------------------------------------------------------------------------- 1 | # Distributed under the OSI-approved BSD 3-Clause License. See accompanying 2 | # file Copyright.txt or https://cmake.org/licensing for details. 3 | 4 | # Copyright 2019 Amine Ben Hassouna 5 | # Copyright 2000-2019 Kitware, Inc. and Contributors 6 | # All rights reserved. 7 | 8 | # Redistribution and use in source and binary forms, with or without 9 | # modification, are permitted provided that the following conditions 10 | # are met: 11 | 12 | # * Redistributions of source code must retain the above copyright 13 | # notice, this list of conditions and the following disclaimer. 14 | 15 | # * Redistributions in binary form must reproduce the above copyright 16 | # notice, this list of conditions and the following disclaimer in the 17 | # documentation and/or other materials provided with the distribution. 18 | 19 | # * Neither the name of Kitware, Inc. nor the names of Contributors 20 | # may be used to endorse or promote products derived from this 21 | # software without specific prior written permission. 22 | 23 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | 35 | #[=======================================================================[.rst: 36 | FindSDL2 37 | -------- 38 | 39 | Locate SDL2 library 40 | 41 | This module defines the following 'IMPORTED' targets: 42 | 43 | :: 44 | 45 | SDL2::Core 46 | The SDL2 library, if found. 47 | Libraries should link to SDL2::Core 48 | 49 | SDL2::Main 50 | The SDL2main library, if found. 51 | Applications should link to SDL2::Main instead of SDL2::Core 52 | 53 | 54 | 55 | This module will set the following variables in your project: 56 | 57 | :: 58 | 59 | SDL2_LIBRARIES, the name of the library to link against 60 | SDL2_INCLUDE_DIRS, where to find SDL.h 61 | SDL2_FOUND, if false, do not try to link to SDL2 62 | SDL2MAIN_FOUND, if false, do not try to link to SDL2main 63 | SDL2_VERSION_STRING, human-readable string containing the version of SDL2 64 | 65 | 66 | 67 | This module responds to the following cache variables: 68 | 69 | :: 70 | 71 | SDL2_PATH 72 | Set a custom SDL2 Library path (default: empty) 73 | 74 | SDL2_NO_DEFAULT_PATH 75 | Disable search SDL2 Library in default path. 76 | If SDL2_PATH (default: ON) 77 | Else (default: OFF) 78 | 79 | SDL2_INCLUDE_DIR 80 | SDL2 headers path. 81 | 82 | SDL2_LIBRARY 83 | SDL2 Library (.dll, .so, .a, etc) path. 84 | 85 | SDL2MAIN_LIBRAY 86 | SDL2main Library (.a) path. 87 | 88 | SDL2_BUILDING_LIBRARY 89 | This flag is useful only when linking to SDL2_LIBRARIES insead of 90 | SDL2::Main. It is required only when building a library that links to 91 | SDL2_LIBRARIES, because only applications need main() (No need to also 92 | link to SDL2main). 93 | If this flag is defined, then no SDL2main will be added to SDL2_LIBRARIES 94 | and no SDL2::Main target will be created. 95 | 96 | 97 | Don't forget to include SDLmain.h and SDLmain.m in your project for the 98 | OS X framework based version. (Other versions link to -lSDL2main which 99 | this module will try to find on your behalf.) Also for OS X, this 100 | module will automatically add the -framework Cocoa on your behalf. 101 | 102 | 103 | Additional Note: If you see an empty SDL2_LIBRARY in your project 104 | configuration, it means CMake did not find your SDL2 library 105 | (SDL2.dll, libsdl2.so, SDL2.framework, etc). Set SDL2_LIBRARY to point 106 | to your SDL2 library, and configure again. Similarly, if you see an 107 | empty SDL2MAIN_LIBRARY, you should set this value as appropriate. These 108 | values are used to generate the final SDL2_LIBRARIES variable and the 109 | SDL2::Core and SDL2::Main targets, but when these values are unset, 110 | SDL2_LIBRARIES, SDL2::Core and SDL2::Main does not get created. 111 | 112 | 113 | $SDL2DIR is an environment variable that would correspond to the 114 | ./configure --prefix=$SDL2DIR used in building SDL2. l.e.galup 9-20-02 115 | 116 | 117 | 118 | Created by Amine Ben Hassouna: 119 | Adapt FindSDL.cmake to SDL2 (FindSDL2.cmake). 120 | Add cache variables for more flexibility: 121 | SDL2_PATH, SDL2_NO_DEFAULT_PATH (for details, see doc above). 122 | Mark 'Threads' as a required dependency for non-OSX systems. 123 | Modernize the FindSDL2.cmake module by creating specific targets: 124 | SDL2::Core and SDL2::Main (for details, see doc above). 125 | 126 | 127 | Original FindSDL.cmake module: 128 | Modified by Eric Wing. Added code to assist with automated building 129 | by using environmental variables and providing a more 130 | controlled/consistent search behavior. Added new modifications to 131 | recognize OS X frameworks and additional Unix paths (FreeBSD, etc). 132 | Also corrected the header search path to follow "proper" SDL 133 | guidelines. Added a search for SDLmain which is needed by some 134 | platforms. Added a search for threads which is needed by some 135 | platforms. Added needed compile switches for MinGW. 136 | 137 | On OSX, this will prefer the Framework version (if found) over others. 138 | People will have to manually change the cache value of SDL2_LIBRARY to 139 | override this selection or set the SDL2_PATH variable or the CMake 140 | environment CMAKE_INCLUDE_PATH to modify the search paths. 141 | 142 | Note that the header path has changed from SDL/SDL.h to just SDL.h 143 | This needed to change because "proper" SDL convention is #include 144 | "SDL.h", not . This is done for portability reasons 145 | because not all systems place things in SDL/ (see FreeBSD). 146 | #]=======================================================================] 147 | 148 | # Define options for searching SDL2 Library in a custom path 149 | 150 | set(SDL2_PATH "" CACHE STRING "Custom SDL2 Library path") 151 | 152 | set(_SDL2_NO_DEFAULT_PATH OFF) 153 | if(SDL2_PATH) 154 | set(_SDL2_NO_DEFAULT_PATH ON) 155 | endif() 156 | 157 | set(SDL2_NO_DEFAULT_PATH ${_SDL2_NO_DEFAULT_PATH} 158 | CACHE BOOL "Disable search SDL2 Library in default path") 159 | unset(_SDL2_NO_DEFAULT_PATH) 160 | 161 | set(SDL2_NO_DEFAULT_PATH_CMD) 162 | if(SDL2_NO_DEFAULT_PATH) 163 | set(SDL2_NO_DEFAULT_PATH_CMD NO_DEFAULT_PATH) 164 | endif() 165 | 166 | # Search for the SDL2 include directory 167 | find_path(SDL2_INCLUDE_DIR SDL.h 168 | HINTS 169 | ENV SDL2DIR 170 | ${SDL2_NO_DEFAULT_PATH_CMD} 171 | PATH_SUFFIXES SDL2 172 | # path suffixes to search inside ENV{SDL2DIR} 173 | include/SDL2 include 174 | PATHS ${SDL2_PATH} 175 | DOC "Where the SDL2 headers can be found" 176 | ) 177 | 178 | set(SDL2_INCLUDE_DIRS "${SDL2_INCLUDE_DIR}") 179 | 180 | if(CMAKE_SIZEOF_VOID_P EQUAL 8) 181 | set(VC_LIB_PATH_SUFFIX lib/x64) 182 | else() 183 | set(VC_LIB_PATH_SUFFIX lib/x86) 184 | endif() 185 | 186 | # SDL-2.0 is the name used by FreeBSD ports... 187 | # don't confuse it for the version number. 188 | find_library(SDL2_LIBRARY 189 | NAMES SDL2 SDL-2.0 190 | HINTS 191 | ENV SDL2DIR 192 | ${SDL2_NO_DEFAULT_PATH_CMD} 193 | PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX} 194 | PATHS ${SDL2_PATH} 195 | DOC "Where the SDL2 Library can be found" 196 | ) 197 | 198 | set(SDL2_LIBRARIES "${SDL2_LIBRARY}") 199 | 200 | if(NOT SDL2_BUILDING_LIBRARY) 201 | if(NOT SDL2_INCLUDE_DIR MATCHES ".framework") 202 | # Non-OS X framework versions expect you to also dynamically link to 203 | # SDL2main. This is mainly for Windows and OS X. Other (Unix) platforms 204 | # seem to provide SDL2main for compatibility even though they don't 205 | # necessarily need it. 206 | 207 | if(SDL2_PATH) 208 | set(SDL2MAIN_LIBRARY_PATHS "${SDL2_PATH}") 209 | endif() 210 | 211 | if(NOT SDL2_NO_DEFAULT_PATH) 212 | set(SDL2MAIN_LIBRARY_PATHS 213 | /sw 214 | /opt/local 215 | /opt/csw 216 | /opt 217 | "${SDL2MAIN_LIBRARY_PATHS}" 218 | ) 219 | endif() 220 | 221 | find_library(SDL2MAIN_LIBRARY 222 | NAMES SDL2main 223 | HINTS 224 | ENV SDL2DIR 225 | ${SDL2_NO_DEFAULT_PATH_CMD} 226 | PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX} 227 | PATHS ${SDL2MAIN_LIBRARY_PATHS} 228 | DOC "Where the SDL2main library can be found" 229 | ) 230 | unset(SDL2MAIN_LIBRARY_PATHS) 231 | endif() 232 | endif() 233 | 234 | # SDL2 may require threads on your system. 235 | # The Apple build may not need an explicit flag because one of the 236 | # frameworks may already provide it. 237 | # But for non-OSX systems, I will use the CMake Threads package. 238 | if(NOT APPLE) 239 | find_package(Threads QUIET) 240 | if(NOT CMAKE_THREAD_LIBS_INIT) 241 | set(SDL2_THREADS_NOT_FOUND "Could NOT find Threads (Threads is required by SDL2).") 242 | if(SDL2_FIND_REQUIRED) 243 | message(FATAL_ERROR ${SDL2_THREADS_NOT_FOUND}) 244 | else() 245 | if(NOT SDL2_FIND_QUIETLY) 246 | message(STATUS ${SDL2_THREADS_NOT_FOUND}) 247 | endif() 248 | return() 249 | endif() 250 | unset(SDL2_THREADS_NOT_FOUND) 251 | endif() 252 | endif() 253 | 254 | # MinGW needs an additional link flag, -mwindows 255 | # It's total link flags should look like -lmingw32 -lSDL2main -lSDL2 -mwindows 256 | if(MINGW) 257 | set(MINGW32_LIBRARY mingw32 "-mwindows" CACHE STRING "link flags for MinGW") 258 | endif() 259 | 260 | if(SDL2_LIBRARY) 261 | # For SDL2main 262 | if(SDL2MAIN_LIBRARY AND NOT SDL2_BUILDING_LIBRARY) 263 | list(FIND SDL2_LIBRARIES "${SDL2MAIN_LIBRARY}" _SDL2_MAIN_INDEX) 264 | if(_SDL2_MAIN_INDEX EQUAL -1) 265 | set(SDL2_LIBRARIES "${SDL2MAIN_LIBRARY}" ${SDL2_LIBRARIES}) 266 | endif() 267 | unset(_SDL2_MAIN_INDEX) 268 | endif() 269 | 270 | # For OS X, SDL2 uses Cocoa as a backend so it must link to Cocoa. 271 | # CMake doesn't display the -framework Cocoa string in the UI even 272 | # though it actually is there if I modify a pre-used variable. 273 | # I think it has something to do with the CACHE STRING. 274 | # So I use a temporary variable until the end so I can set the 275 | # "real" variable in one-shot. 276 | if(APPLE) 277 | set(SDL2_LIBRARIES ${SDL2_LIBRARIES} "-framework Cocoa") 278 | endif() 279 | 280 | # For threads, as mentioned Apple doesn't need this. 281 | # In fact, there seems to be a problem if I used the Threads package 282 | # and try using this line, so I'm just skipping it entirely for OS X. 283 | if(NOT APPLE) 284 | set(SDL2_LIBRARIES ${SDL2_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) 285 | endif() 286 | 287 | # For MinGW library 288 | if(MINGW) 289 | set(SDL2_LIBRARIES ${MINGW32_LIBRARY} ${SDL2_LIBRARIES}) 290 | endif() 291 | 292 | endif() 293 | 294 | # Read SDL2 version 295 | if(SDL2_INCLUDE_DIR AND EXISTS "${SDL2_INCLUDE_DIR}/SDL_version.h") 296 | file(STRINGS "${SDL2_INCLUDE_DIR}/SDL_version.h" SDL2_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL_MAJOR_VERSION[ \t]+[0-9]+$") 297 | file(STRINGS "${SDL2_INCLUDE_DIR}/SDL_version.h" SDL2_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL_MINOR_VERSION[ \t]+[0-9]+$") 298 | file(STRINGS "${SDL2_INCLUDE_DIR}/SDL_version.h" SDL2_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL_PATCHLEVEL[ \t]+[0-9]+$") 299 | string(REGEX REPLACE "^#define[ \t]+SDL_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_VERSION_MAJOR "${SDL2_VERSION_MAJOR_LINE}") 300 | string(REGEX REPLACE "^#define[ \t]+SDL_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_VERSION_MINOR "${SDL2_VERSION_MINOR_LINE}") 301 | string(REGEX REPLACE "^#define[ \t]+SDL_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL2_VERSION_PATCH "${SDL2_VERSION_PATCH_LINE}") 302 | set(SDL2_VERSION_STRING ${SDL2_VERSION_MAJOR}.${SDL2_VERSION_MINOR}.${SDL2_VERSION_PATCH}) 303 | unset(SDL2_VERSION_MAJOR_LINE) 304 | unset(SDL2_VERSION_MINOR_LINE) 305 | unset(SDL2_VERSION_PATCH_LINE) 306 | unset(SDL2_VERSION_MAJOR) 307 | unset(SDL2_VERSION_MINOR) 308 | unset(SDL2_VERSION_PATCH) 309 | endif() 310 | 311 | include(FindPackageHandleStandardArgs) 312 | 313 | FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2 314 | REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR 315 | VERSION_VAR SDL2_VERSION_STRING) 316 | 317 | if(SDL2MAIN_LIBRARY) 318 | FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2main 319 | REQUIRED_VARS SDL2MAIN_LIBRARY SDL2_INCLUDE_DIR 320 | VERSION_VAR SDL2_VERSION_STRING) 321 | endif() 322 | 323 | 324 | mark_as_advanced(SDL2_PATH 325 | SDL2_NO_DEFAULT_PATH 326 | SDL2_LIBRARY 327 | SDL2MAIN_LIBRARY 328 | SDL2_INCLUDE_DIR 329 | SDL2_BUILDING_LIBRARY) 330 | 331 | 332 | # SDL2:: targets (SDL2::Core and SDL2::Main) 333 | if(SDL2_FOUND) 334 | 335 | # SDL2::Core target 336 | if(SDL2_LIBRARY AND NOT TARGET SDL2::Core) 337 | add_library(SDL2::Core UNKNOWN IMPORTED) 338 | set_target_properties(SDL2::Core PROPERTIES 339 | IMPORTED_LOCATION "${SDL2_LIBRARY}" 340 | INTERFACE_INCLUDE_DIRECTORIES "${SDL2_INCLUDE_DIR}") 341 | 342 | if(APPLE) 343 | # For OS X, SDL2 uses Cocoa as a backend so it must link to Cocoa. 344 | # For more details, please see above. 345 | set_property(TARGET SDL2::Core APPEND PROPERTY 346 | INTERFACE_LINK_OPTIONS "-framework Cocoa") 347 | else() 348 | # For threads, as mentioned Apple doesn't need this. 349 | # For more details, please see above. 350 | set_property(TARGET SDL2::Core APPEND PROPERTY 351 | INTERFACE_LINK_LIBRARIES Threads::Threads) 352 | endif() 353 | endif() 354 | 355 | # SDL2::Main target 356 | # Applications should link to SDL2::Main instead of SDL2::Core 357 | # For more details, please see above. 358 | if(NOT SDL2_BUILDING_LIBRARY AND NOT TARGET SDL2::Main) 359 | 360 | if(SDL2_INCLUDE_DIR MATCHES ".framework" OR NOT SDL2MAIN_LIBRARY) 361 | add_library(SDL2::Main INTERFACE IMPORTED) 362 | set_property(TARGET SDL2::Main PROPERTY 363 | INTERFACE_LINK_LIBRARIES SDL2::Core) 364 | elseif(SDL2MAIN_LIBRARY) 365 | # MinGW requires that the mingw32 library is specified before the 366 | # libSDL2main.a static library when linking. 367 | # The SDL2::MainInternal target is used internally to make sure that 368 | # CMake respects this condition. 369 | add_library(SDL2::MainInternal UNKNOWN IMPORTED) 370 | set_property(TARGET SDL2::MainInternal PROPERTY 371 | IMPORTED_LOCATION "${SDL2MAIN_LIBRARY}") 372 | set_property(TARGET SDL2::MainInternal PROPERTY 373 | INTERFACE_LINK_LIBRARIES SDL2::Core) 374 | 375 | add_library(SDL2::Main INTERFACE IMPORTED) 376 | 377 | if(MINGW) 378 | # MinGW needs an additional link flag '-mwindows' and link to mingw32 379 | set_property(TARGET SDL2::Main PROPERTY 380 | INTERFACE_LINK_LIBRARIES "mingw32" "-mwindows") 381 | endif() 382 | 383 | set_property(TARGET SDL2::Main APPEND PROPERTY 384 | INTERFACE_LINK_LIBRARIES SDL2::MainInternal) 385 | endif() 386 | 387 | endif() 388 | endif() 389 | -------------------------------------------------------------------------------- /cmake/sdl2/FindSDL2_gfx.cmake: -------------------------------------------------------------------------------- 1 | # Distributed under the OSI-approved BSD 3-Clause License. See accompanying 2 | # file Copyright.txt or https://cmake.org/licensing for details. 3 | 4 | # Copyright 2019 Amine Ben Hassouna 5 | # Copyright 2000-2019 Kitware, Inc. and Contributors 6 | # All rights reserved. 7 | 8 | # Redistribution and use in source and binary forms, with or without 9 | # modification, are permitted provided that the following conditions 10 | # are met: 11 | 12 | # * Redistributions of source code must retain the above copyright 13 | # notice, this list of conditions and the following disclaimer. 14 | 15 | # * Redistributions in binary form must reproduce the above copyright 16 | # notice, this list of conditions and the following disclaimer in the 17 | # documentation and/or other materials provided with the distribution. 18 | 19 | # * Neither the name of Kitware, Inc. nor the names of Contributors 20 | # may be used to endorse or promote products derived from this 21 | # software without specific prior written permission. 22 | 23 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | 35 | #[=======================================================================[.rst: 36 | FindSDL2_gfx 37 | -------------- 38 | 39 | Locate SDL2_gfx library 40 | 41 | This module defines the following 'IMPORTED' target: 42 | 43 | :: 44 | 45 | SDL2::GFX 46 | The SDL2_gfx library, if found. 47 | Have SDL2::Core as a link dependency. 48 | 49 | 50 | 51 | This module will set the following variables in your project: 52 | 53 | :: 54 | 55 | SDL2_GFX_LIBRARIES, the name of the library to link against 56 | SDL2_GFX_INCLUDE_DIRS, where to find the headers 57 | SDL2_GFX_FOUND, if false, do not try to link against 58 | SDL2_GFX_VERSION_STRING - human-readable string containing the 59 | version of SDL2_gfx 60 | 61 | 62 | 63 | This module responds to the following cache variables: 64 | 65 | :: 66 | 67 | SDL2_GFX_PATH 68 | Set a custom SDL2_gfx Library path (default: empty) 69 | 70 | SDL2_GFX_NO_DEFAULT_PATH 71 | Disable search SDL2_gfx Library in default path. 72 | If SDL2_GFX_PATH (default: ON) 73 | Else (default: OFF) 74 | 75 | SDL2_GFX_INCLUDE_DIR 76 | SDL2_gfx headers path. 77 | 78 | SDL2_GFX_LIBRARY 79 | SDL2_gfx Library (.dll, .so, .a, etc) path. 80 | 81 | 82 | Additional Note: If you see an empty SDL2_GFX_LIBRARY in your project 83 | configuration, it means CMake did not find your SDL2_gfx library 84 | (SDL2_gfx.dll, libsdl2_gfx.so, etc). Set SDL2_GFX_LIBRARY to point 85 | to your SDL2_gfx library, and configure again. This value is used to 86 | generate the final SDL2_GFX_LIBRARIES variable and the SDL2::GFX target, 87 | but when this value is unset, SDL2_GFX_LIBRARIES and SDL2::GFX does not 88 | get created. 89 | 90 | 91 | $SDL2GFXDIR is an environment variable that would correspond to the 92 | ./configure --prefix=$SDL2GFXDIR used in building SDL2_gfx. 93 | 94 | $SDL2DIR is an environment variable that would correspond to the 95 | ./configure --prefix=$SDL2DIR used in building SDL2. 96 | 97 | 98 | 99 | Created by Amine Ben Hassouna: 100 | Adapt FindSDL_image.cmake to SDL2_gfx (FindSDL2_gfx.cmake). 101 | Add cache variables for more flexibility: 102 | SDL2_GFX_PATH, SDL2_GFX_NO_DEFAULT_PATH (for details, see doc above). 103 | Add SDL2 as a required dependency. 104 | Modernize the FindSDL2_gfx.cmake module by creating a specific target: 105 | SDL2::GFX (for details, see doc above). 106 | 107 | Original FindSDL_image.cmake module: 108 | Created by Eric Wing. This was influenced by the FindSDL.cmake 109 | module, but with modifications to recognize OS X frameworks and 110 | additional Unix paths (FreeBSD, etc). 111 | #]=======================================================================] 112 | 113 | # SDL2 Library required 114 | find_package(SDL2 QUIET) 115 | if(NOT SDL2_FOUND) 116 | set(SDL2_GFX_SDL2_NOT_FOUND "Could NOT find SDL2 (SDL2 is required by SDL2_gfx).") 117 | if(SDL2_gfx_FIND_REQUIRED) 118 | message(FATAL_ERROR ${SDL2_GFX_SDL2_NOT_FOUND}) 119 | else() 120 | if(NOT SDL2_gfx_FIND_QUIETLY) 121 | message(STATUS ${SDL2_GFX_SDL2_NOT_FOUND}) 122 | endif() 123 | return() 124 | endif() 125 | unset(SDL2_GFX_SDL2_NOT_FOUND) 126 | endif() 127 | 128 | 129 | # Define options for searching SDL2_gfx Library in a custom path 130 | 131 | set(SDL2_GFX_PATH "" CACHE STRING "Custom SDL2_gfx Library path") 132 | 133 | set(_SDL2_GFX_NO_DEFAULT_PATH OFF) 134 | if(SDL2_GFX_PATH) 135 | set(_SDL2_GFX_NO_DEFAULT_PATH ON) 136 | endif() 137 | 138 | set(SDL2_GFX_NO_DEFAULT_PATH ${_SDL2_GFX_NO_DEFAULT_PATH} 139 | CACHE BOOL "Disable search SDL2_gfx Library in default path") 140 | unset(_SDL2_GFX_NO_DEFAULT_PATH) 141 | 142 | set(SDL2_GFX_NO_DEFAULT_PATH_CMD) 143 | if(SDL2_GFX_NO_DEFAULT_PATH) 144 | set(SDL2_GFX_NO_DEFAULT_PATH_CMD NO_DEFAULT_PATH) 145 | endif() 146 | 147 | # Search for the SDL2_gfx include directory 148 | find_path(SDL2_GFX_INCLUDE_DIR SDL2_gfxPrimitives.h 149 | HINTS 150 | ENV SDL2GFXDIR 151 | ENV SDL2DIR 152 | ${SDL2_GFX_NO_DEFAULT_PATH_CMD} 153 | PATH_SUFFIXES SDL2 154 | # path suffixes to search inside ENV{SDL2DIR} 155 | # and ENV{SDL2GFXDIR} 156 | include/SDL2 include 157 | PATHS ${SDL2_GFX_PATH} 158 | DOC "Where the SDL2_gfx headers can be found" 159 | ) 160 | 161 | if(CMAKE_SIZEOF_VOID_P EQUAL 8) 162 | set(VC_LIB_PATH_SUFFIX lib/x64) 163 | else() 164 | set(VC_LIB_PATH_SUFFIX lib/x86) 165 | endif() 166 | 167 | # Search for the SDL2_gfx library 168 | find_library(SDL2_GFX_LIBRARY 169 | NAMES SDL2_gfx 170 | HINTS 171 | ENV SDL2GFXDIR 172 | ENV SDL2DIR 173 | ${SDL2_GFX_NO_DEFAULT_PATH_CMD} 174 | PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX} 175 | PATHS ${SDL2_GFX_PATH} 176 | DOC "Where the SDL2_gfx Library can be found" 177 | ) 178 | 179 | # Read SDL2_gfx version 180 | if(SDL2_GFX_INCLUDE_DIR AND EXISTS "${SDL2_GFX_INCLUDE_DIR}/SDL2_gfxPrimitives.h") 181 | file(STRINGS "${SDL2_GFX_INCLUDE_DIR}/SDL2_gfxPrimitives.h" SDL2_GFX_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL2_GFXPRIMITIVES_MAJOR[ \t]+[0-9]+$") 182 | file(STRINGS "${SDL2_GFX_INCLUDE_DIR}/SDL2_gfxPrimitives.h" SDL2_GFX_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL2_GFXPRIMITIVES_MINOR[ \t]+[0-9]+$") 183 | file(STRINGS "${SDL2_GFX_INCLUDE_DIR}/SDL2_gfxPrimitives.h" SDL2_GFX_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL2_GFXPRIMITIVES_MICRO[ \t]+[0-9]+$") 184 | string(REGEX REPLACE "^#define[ \t]+SDL2_GFXPRIMITIVES_MAJOR[ \t]+([0-9]+)$" "\\1" SDL2_GFX_VERSION_MAJOR "${SDL2_GFX_VERSION_MAJOR_LINE}") 185 | string(REGEX REPLACE "^#define[ \t]+SDL2_GFXPRIMITIVES_MINOR[ \t]+([0-9]+)$" "\\1" SDL2_GFX_VERSION_MINOR "${SDL2_GFX_VERSION_MINOR_LINE}") 186 | string(REGEX REPLACE "^#define[ \t]+SDL2_GFXPRIMITIVES_MICRO[ \t]+([0-9]+)$" "\\1" SDL2_GFX_VERSION_PATCH "${SDL2_GFX_VERSION_PATCH_LINE}") 187 | set(SDL2_GFX_VERSION_STRING ${SDL2_GFX_VERSION_MAJOR}.${SDL2_GFX_VERSION_MINOR}.${SDL2_GFX_VERSION_PATCH}) 188 | unset(SDL2_GFX_VERSION_MAJOR_LINE) 189 | unset(SDL2_GFX_VERSION_MINOR_LINE) 190 | unset(SDL2_GFX_VERSION_PATCH_LINE) 191 | unset(SDL2_GFX_VERSION_MAJOR) 192 | unset(SDL2_GFX_VERSION_MINOR) 193 | unset(SDL2_GFX_VERSION_PATCH) 194 | endif() 195 | 196 | set(SDL2_GFX_LIBRARIES ${SDL2_GFX_LIBRARY}) 197 | set(SDL2_GFX_INCLUDE_DIRS ${SDL2_GFX_INCLUDE_DIR}) 198 | 199 | include(FindPackageHandleStandardArgs) 200 | 201 | FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2_gfx 202 | REQUIRED_VARS SDL2_GFX_LIBRARIES SDL2_GFX_INCLUDE_DIRS 203 | VERSION_VAR SDL2_GFX_VERSION_STRING) 204 | 205 | 206 | mark_as_advanced(SDL2_GFX_PATH 207 | SDL2_GFX_NO_DEFAULT_PATH 208 | SDL2_GFX_LIBRARY 209 | SDL2_GFX_INCLUDE_DIR) 210 | 211 | 212 | if(SDL2_GFX_FOUND) 213 | 214 | # SDL2::GFX target 215 | if(SDL2_GFX_LIBRARY AND NOT TARGET SDL2::GFX) 216 | add_library(SDL2::GFX UNKNOWN IMPORTED) 217 | set_target_properties(SDL2::GFX PROPERTIES 218 | IMPORTED_LOCATION "${SDL2_GFX_LIBRARY}" 219 | INTERFACE_INCLUDE_DIRECTORIES "${SDL2_GFX_INCLUDE_DIR}" 220 | INTERFACE_LINK_LIBRARIES SDL2::Core) 221 | endif() 222 | endif() 223 | -------------------------------------------------------------------------------- /cmake/sdl2/FindSDL2_image.cmake: -------------------------------------------------------------------------------- 1 | # Distributed under the OSI-approved BSD 3-Clause License. See accompanying 2 | # file Copyright.txt or https://cmake.org/licensing for details. 3 | 4 | # Copyright 2019 Amine Ben Hassouna 5 | # Copyright 2000-2019 Kitware, Inc. and Contributors 6 | # All rights reserved. 7 | 8 | # Redistribution and use in source and binary forms, with or without 9 | # modification, are permitted provided that the following conditions 10 | # are met: 11 | 12 | # * Redistributions of source code must retain the above copyright 13 | # notice, this list of conditions and the following disclaimer. 14 | 15 | # * Redistributions in binary form must reproduce the above copyright 16 | # notice, this list of conditions and the following disclaimer in the 17 | # documentation and/or other materials provided with the distribution. 18 | 19 | # * Neither the name of Kitware, Inc. nor the names of Contributors 20 | # may be used to endorse or promote products derived from this 21 | # software without specific prior written permission. 22 | 23 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | 35 | #[=======================================================================[.rst: 36 | FindSDL2_image 37 | -------------- 38 | 39 | Locate SDL2_image library 40 | 41 | This module defines the following 'IMPORTED' target: 42 | 43 | :: 44 | 45 | SDL2::Image 46 | The SDL2_image library, if found. 47 | Have SDL2::Core as a link dependency. 48 | 49 | 50 | 51 | This module will set the following variables in your project: 52 | 53 | :: 54 | 55 | SDL2_IMAGE_LIBRARIES, the name of the library to link against 56 | SDL2_IMAGE_INCLUDE_DIRS, where to find the headers 57 | SDL2_IMAGE_FOUND, if false, do not try to link against 58 | SDL2_IMAGE_VERSION_STRING - human-readable string containing the 59 | version of SDL2_image 60 | 61 | 62 | 63 | This module responds to the following cache variables: 64 | 65 | :: 66 | 67 | SDL2_IMAGE_PATH 68 | Set a custom SDL2_image Library path (default: empty) 69 | 70 | SDL2_IMAGE_NO_DEFAULT_PATH 71 | Disable search SDL2_image Library in default path. 72 | If SDL2_IMAGE_PATH (default: ON) 73 | Else (default: OFF) 74 | 75 | SDL2_IMAGE_INCLUDE_DIR 76 | SDL2_image headers path. 77 | 78 | SDL2_IMAGE_LIBRARY 79 | SDL2_image Library (.dll, .so, .a, etc) path. 80 | 81 | 82 | Additional Note: If you see an empty SDL2_IMAGE_LIBRARY in your project 83 | configuration, it means CMake did not find your SDL2_image library 84 | (SDL2_image.dll, libsdl2_image.so, etc). Set SDL2_IMAGE_LIBRARY to point 85 | to your SDL2_image library, and configure again. This value is used to 86 | generate the final SDL2_IMAGE_LIBRARIES variable and the SDL2::Image target, 87 | but when this value is unset, SDL2_IMAGE_LIBRARIES and SDL2::Image does not 88 | get created. 89 | 90 | 91 | $SDL2IMAGEDIR is an environment variable that would correspond to the 92 | ./configure --prefix=$SDL2IMAGEDIR used in building SDL2_image. 93 | 94 | $SDL2DIR is an environment variable that would correspond to the 95 | ./configure --prefix=$SDL2DIR used in building SDL2. 96 | 97 | 98 | 99 | Created by Amine Ben Hassouna: 100 | Adapt FindSDL_image.cmake to SDL2_image (FindSDL2_image.cmake). 101 | Add cache variables for more flexibility: 102 | SDL2_IMAGE_PATH, SDL2_IMAGE_NO_DEFAULT_PATH (for details, see doc above). 103 | Add SDL2 as a required dependency. 104 | Modernize the FindSDL2_image.cmake module by creating a specific target: 105 | SDL2::Image (for details, see doc above). 106 | 107 | Original FindSDL_image.cmake module: 108 | Created by Eric Wing. This was influenced by the FindSDL.cmake 109 | module, but with modifications to recognize OS X frameworks and 110 | additional Unix paths (FreeBSD, etc). 111 | #]=======================================================================] 112 | 113 | # SDL2 Library required 114 | find_package(SDL2 QUIET) 115 | if(NOT SDL2_FOUND) 116 | set(SDL2_IMAGE_SDL2_NOT_FOUND "Could NOT find SDL2 (SDL2 is required by SDL2_image).") 117 | if(SDL2_image_FIND_REQUIRED) 118 | message(FATAL_ERROR ${SDL2_IMAGE_SDL2_NOT_FOUND}) 119 | else() 120 | if(NOT SDL2_image_FIND_QUIETLY) 121 | message(STATUS ${SDL2_IMAGE_SDL2_NOT_FOUND}) 122 | endif() 123 | return() 124 | endif() 125 | unset(SDL2_IMAGE_SDL2_NOT_FOUND) 126 | endif() 127 | 128 | 129 | # Define options for searching SDL2_image Library in a custom path 130 | 131 | set(SDL2_IMAGE_PATH "" CACHE STRING "Custom SDL2_image Library path") 132 | 133 | set(_SDL2_IMAGE_NO_DEFAULT_PATH OFF) 134 | if(SDL2_IMAGE_PATH) 135 | set(_SDL2_IMAGE_NO_DEFAULT_PATH ON) 136 | endif() 137 | 138 | set(SDL2_IMAGE_NO_DEFAULT_PATH ${_SDL2_IMAGE_NO_DEFAULT_PATH} 139 | CACHE BOOL "Disable search SDL2_image Library in default path") 140 | unset(_SDL2_IMAGE_NO_DEFAULT_PATH) 141 | 142 | set(SDL2_IMAGE_NO_DEFAULT_PATH_CMD) 143 | if(SDL2_IMAGE_NO_DEFAULT_PATH) 144 | set(SDL2_IMAGE_NO_DEFAULT_PATH_CMD NO_DEFAULT_PATH) 145 | endif() 146 | 147 | # Search for the SDL2_image include directory 148 | find_path(SDL2_IMAGE_INCLUDE_DIR SDL_image.h 149 | HINTS 150 | ENV SDL2IMAGEDIR 151 | ENV SDL2DIR 152 | ${SDL2_IMAGE_NO_DEFAULT_PATH_CMD} 153 | PATH_SUFFIXES SDL2 154 | # path suffixes to search inside ENV{SDL2DIR} 155 | # and ENV{SDL2IMAGEDIR} 156 | include/SDL2 include 157 | PATHS ${SDL2_IMAGE_PATH} 158 | DOC "Where the SDL2_image headers can be found" 159 | ) 160 | 161 | if(CMAKE_SIZEOF_VOID_P EQUAL 8) 162 | set(VC_LIB_PATH_SUFFIX lib/x64) 163 | else() 164 | set(VC_LIB_PATH_SUFFIX lib/x86) 165 | endif() 166 | 167 | # Search for the SDL2_image library 168 | find_library(SDL2_IMAGE_LIBRARY 169 | NAMES SDL2_image 170 | HINTS 171 | ENV SDL2IMAGEDIR 172 | ENV SDL2DIR 173 | ${SDL2_IMAGE_NO_DEFAULT_PATH_CMD} 174 | PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX} 175 | PATHS ${SDL2_IMAGE_PATH} 176 | DOC "Where the SDL2_image Library can be found" 177 | ) 178 | 179 | # Read SDL2_image version 180 | if(SDL2_IMAGE_INCLUDE_DIR AND EXISTS "${SDL2_IMAGE_INCLUDE_DIR}/SDL_image.h") 181 | file(STRINGS "${SDL2_IMAGE_INCLUDE_DIR}/SDL_image.h" SDL2_IMAGE_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL_IMAGE_MAJOR_VERSION[ \t]+[0-9]+$") 182 | file(STRINGS "${SDL2_IMAGE_INCLUDE_DIR}/SDL_image.h" SDL2_IMAGE_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL_IMAGE_MINOR_VERSION[ \t]+[0-9]+$") 183 | file(STRINGS "${SDL2_IMAGE_INCLUDE_DIR}/SDL_image.h" SDL2_IMAGE_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL_IMAGE_PATCHLEVEL[ \t]+[0-9]+$") 184 | string(REGEX REPLACE "^#define[ \t]+SDL_IMAGE_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_IMAGE_VERSION_MAJOR "${SDL2_IMAGE_VERSION_MAJOR_LINE}") 185 | string(REGEX REPLACE "^#define[ \t]+SDL_IMAGE_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_IMAGE_VERSION_MINOR "${SDL2_IMAGE_VERSION_MINOR_LINE}") 186 | string(REGEX REPLACE "^#define[ \t]+SDL_IMAGE_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL2_IMAGE_VERSION_PATCH "${SDL2_IMAGE_VERSION_PATCH_LINE}") 187 | set(SDL2_IMAGE_VERSION_STRING ${SDL2_IMAGE_VERSION_MAJOR}.${SDL2_IMAGE_VERSION_MINOR}.${SDL2_IMAGE_VERSION_PATCH}) 188 | unset(SDL2_IMAGE_VERSION_MAJOR_LINE) 189 | unset(SDL2_IMAGE_VERSION_MINOR_LINE) 190 | unset(SDL2_IMAGE_VERSION_PATCH_LINE) 191 | unset(SDL2_IMAGE_VERSION_MAJOR) 192 | unset(SDL2_IMAGE_VERSION_MINOR) 193 | unset(SDL2_IMAGE_VERSION_PATCH) 194 | endif() 195 | 196 | set(SDL2_IMAGE_LIBRARIES ${SDL2_IMAGE_LIBRARY}) 197 | set(SDL2_IMAGE_INCLUDE_DIRS ${SDL2_IMAGE_INCLUDE_DIR}) 198 | 199 | include(FindPackageHandleStandardArgs) 200 | 201 | FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2_image 202 | REQUIRED_VARS SDL2_IMAGE_LIBRARIES SDL2_IMAGE_INCLUDE_DIRS 203 | VERSION_VAR SDL2_IMAGE_VERSION_STRING) 204 | 205 | 206 | mark_as_advanced(SDL2_IMAGE_PATH 207 | SDL2_IMAGE_NO_DEFAULT_PATH 208 | SDL2_IMAGE_LIBRARY 209 | SDL2_IMAGE_INCLUDE_DIR) 210 | 211 | 212 | if(SDL2_IMAGE_FOUND) 213 | 214 | # SDL2::Image target 215 | if(SDL2_IMAGE_LIBRARY AND NOT TARGET SDL2::Image) 216 | add_library(SDL2::Image UNKNOWN IMPORTED) 217 | set_target_properties(SDL2::Image PROPERTIES 218 | IMPORTED_LOCATION "${SDL2_IMAGE_LIBRARY}" 219 | INTERFACE_INCLUDE_DIRECTORIES "${SDL2_IMAGE_INCLUDE_DIR}" 220 | INTERFACE_LINK_LIBRARIES SDL2::Core) 221 | endif() 222 | endif() 223 | -------------------------------------------------------------------------------- /cmake/sdl2/FindSDL2_mixer.cmake: -------------------------------------------------------------------------------- 1 | # Distributed under the OSI-approved BSD 3-Clause License. See accompanying 2 | # file Copyright.txt or https://cmake.org/licensing for details. 3 | 4 | # Copyright 2019 Amine Ben Hassouna 5 | # Copyright 2000-2019 Kitware, Inc. and Contributors 6 | # All rights reserved. 7 | 8 | # Redistribution and use in source and binary forms, with or without 9 | # modification, are permitted provided that the following conditions 10 | # are met: 11 | 12 | # * Redistributions of source code must retain the above copyright 13 | # notice, this list of conditions and the following disclaimer. 14 | 15 | # * Redistributions in binary form must reproduce the above copyright 16 | # notice, this list of conditions and the following disclaimer in the 17 | # documentation and/or other materials provided with the distribution. 18 | 19 | # * Neither the name of Kitware, Inc. nor the names of Contributors 20 | # may be used to endorse or promote products derived from this 21 | # software without specific prior written permission. 22 | 23 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | 35 | #[=======================================================================[.rst: 36 | FindSDL2_mixer 37 | -------------- 38 | 39 | Locate SDL2_mixer library 40 | 41 | This module defines the following 'IMPORTED' target: 42 | 43 | :: 44 | 45 | SDL2::Mixer 46 | The SDL2_mixer library, if found. 47 | Have SDL2::Core as a link dependency. 48 | 49 | 50 | 51 | This module will set the following variables in your project: 52 | 53 | :: 54 | 55 | SDL2_MIXER_LIBRARIES, the name of the library to link against 56 | SDL2_MIXER_INCLUDE_DIRS, where to find the headers 57 | SDL2_MIXER_FOUND, if false, do not try to link against 58 | SDL2_MIXER_VERSION_STRING - human-readable string containing the 59 | version of SDL2_mixer 60 | 61 | This module responds to the following cache variables: 62 | 63 | :: 64 | 65 | SDL2_MIXER_PATH 66 | Set a custom SDL2_mixer Library path (default: empty) 67 | 68 | SDL2_MIXER_NO_DEFAULT_PATH 69 | Disable search SDL2_mixer Library in default path. 70 | If SDL2_MIXER_PATH (default: ON) 71 | Else (default: OFF) 72 | 73 | SDL2_MIXER_INCLUDE_DIR 74 | SDL2_mixer headers path. 75 | 76 | SDL2_MIXER_LIBRARY 77 | SDL2_mixer Library (.dll, .so, .a, etc) path. 78 | 79 | 80 | Additional Note: If you see an empty SDL2_MIXER_LIBRARY in your project 81 | configuration, it means CMake did not find your SDL2_mixer library 82 | (SDL2_mixer.dll, libsdl2_mixer.so, etc). Set SDL2_MIXER_LIBRARY to point 83 | to your SDL2_mixer library, and configure again. This value is used to 84 | generate the final SDL2_MIXER_LIBRARIES variable and the SDL2::Mixer target, 85 | but when this value is unset, SDL2_MIXER_LIBRARIES and SDL2::Mixer does not 86 | get created. 87 | 88 | 89 | $SDL2MIXERDIR is an environment variable that would correspond to the 90 | ./configure --prefix=$SDL2MIXERDIR used in building SDL2_mixer. 91 | 92 | $SDL2DIR is an environment variable that would correspond to the 93 | ./configure --prefix=$SDL2DIR used in building SDL2. 94 | 95 | 96 | 97 | Created by Amine Ben Hassouna: 98 | Adapt FindSDL_mixer.cmake to SDL2_mixer (FindSDL2_mixer.cmake). 99 | Add cache variables for more flexibility: 100 | SDL2_MIXER_PATH, SDL2_MIXER_NO_DEFAULT_PATH (for details, see doc above). 101 | Add SDL2 as a required dependency. 102 | Modernize the FindSDL2_mixer.cmake module by creating a specific target: 103 | SDL2::Mixer (for details, see doc above). 104 | 105 | Original FindSDL_mixer.cmake module: 106 | Created by Eric Wing. This was influenced by the FindSDL.cmake 107 | module, but with modifications to recognize OS X frameworks and 108 | additional Unix paths (FreeBSD, etc). 109 | #]=======================================================================] 110 | 111 | # SDL2 Library required 112 | find_package(SDL2 QUIET) 113 | if(NOT SDL2_FOUND) 114 | set(SDL2_MIXER_SDL2_NOT_FOUND "Could NOT find SDL2 (SDL2 is required by SDL2_mixer).") 115 | if(SDL2_mixer_FIND_REQUIRED) 116 | message(FATAL_ERROR ${SDL2_MIXER_SDL2_NOT_FOUND}) 117 | else() 118 | if(NOT SDL2_mixer_FIND_QUIETLY) 119 | message(STATUS ${SDL2_MIXER_SDL2_NOT_FOUND}) 120 | endif() 121 | return() 122 | endif() 123 | unset(SDL2_MIXER_SDL2_NOT_FOUND) 124 | endif() 125 | 126 | 127 | # Define options for searching SDL2_mixer Library in a custom path 128 | 129 | set(SDL2_MIXER_PATH "" CACHE STRING "Custom SDL2_mixer Library path") 130 | 131 | set(_SDL2_MIXER_NO_DEFAULT_PATH OFF) 132 | if(SDL2_MIXER_PATH) 133 | set(_SDL2_MIXER_NO_DEFAULT_PATH ON) 134 | endif() 135 | 136 | set(SDL2_MIXER_NO_DEFAULT_PATH ${_SDL2_MIXER_NO_DEFAULT_PATH} 137 | CACHE BOOL "Disable search SDL2_mixer Library in default path") 138 | unset(_SDL2_MIXER_NO_DEFAULT_PATH) 139 | 140 | set(SDL2_MIXER_NO_DEFAULT_PATH_CMD) 141 | if(SDL2_MIXER_NO_DEFAULT_PATH) 142 | set(SDL2_MIXER_NO_DEFAULT_PATH_CMD NO_DEFAULT_PATH) 143 | endif() 144 | 145 | # Search for the SDL2_mixer include directory 146 | find_path(SDL2_MIXER_INCLUDE_DIR SDL_mixer.h 147 | HINTS 148 | ENV SDL2MIXERDIR 149 | ENV SDL2DIR 150 | ${SDL2_MIXER_NO_DEFAULT_PATH_CMD} 151 | PATH_SUFFIXES SDL2 152 | # path suffixes to search inside ENV{SDL2DIR} 153 | # and ENV{SDL2MIXERDIR} 154 | include/SDL2 include 155 | PATHS ${SDL2_MIXER_PATH} 156 | DOC "Where the SDL2_mixer headers can be found" 157 | ) 158 | 159 | if(CMAKE_SIZEOF_VOID_P EQUAL 8) 160 | set(VC_LIB_PATH_SUFFIX lib/x64) 161 | else() 162 | set(VC_LIB_PATH_SUFFIX lib/x86) 163 | endif() 164 | 165 | # Search for the SDL2_mixer library 166 | find_library(SDL2_MIXER_LIBRARY 167 | NAMES SDL2_mixer 168 | HINTS 169 | ENV SDL2MIXERDIR 170 | ENV SDL2DIR 171 | ${SDL2_MIXER_NO_DEFAULT_PATH_CMD} 172 | PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX} 173 | PATHS ${SDL2_MIXER_PATH} 174 | DOC "Where the SDL2_mixer Library can be found" 175 | ) 176 | 177 | # Read SDL2_mixer version 178 | if(SDL2_MIXER_INCLUDE_DIR AND EXISTS "${SDL2_MIXER_INCLUDE_DIR}/SDL_mixer.h") 179 | file(STRINGS "${SDL2_MIXER_INCLUDE_DIR}/SDL_mixer.h" SDL2_MIXER_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL_MIXER_MAJOR_VERSION[ \t]+[0-9]+$") 180 | file(STRINGS "${SDL2_MIXER_INCLUDE_DIR}/SDL_mixer.h" SDL2_MIXER_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL_MIXER_MINOR_VERSION[ \t]+[0-9]+$") 181 | file(STRINGS "${SDL2_MIXER_INCLUDE_DIR}/SDL_mixer.h" SDL2_MIXER_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL_MIXER_PATCHLEVEL[ \t]+[0-9]+$") 182 | string(REGEX REPLACE "^#define[ \t]+SDL_MIXER_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_MIXER_VERSION_MAJOR "${SDL2_MIXER_VERSION_MAJOR_LINE}") 183 | string(REGEX REPLACE "^#define[ \t]+SDL_MIXER_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_MIXER_VERSION_MINOR "${SDL2_MIXER_VERSION_MINOR_LINE}") 184 | string(REGEX REPLACE "^#define[ \t]+SDL_MIXER_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL2_MIXER_VERSION_PATCH "${SDL2_MIXER_VERSION_PATCH_LINE}") 185 | set(SDL2_MIXER_VERSION_STRING ${SDL2_MIXER_VERSION_MAJOR}.${SDL2_MIXER_VERSION_MINOR}.${SDL2_MIXER_VERSION_PATCH}) 186 | unset(SDL2_MIXER_VERSION_MAJOR_LINE) 187 | unset(SDL2_MIXER_VERSION_MINOR_LINE) 188 | unset(SDL2_MIXER_VERSION_PATCH_LINE) 189 | unset(SDL2_MIXER_VERSION_MAJOR) 190 | unset(SDL2_MIXER_VERSION_MINOR) 191 | unset(SDL2_MIXER_VERSION_PATCH) 192 | endif() 193 | 194 | set(SDL2_MIXER_LIBRARIES ${SDL2_MIXER_LIBRARY}) 195 | set(SDL2_MIXER_INCLUDE_DIRS ${SDL2_MIXER_INCLUDE_DIR}) 196 | 197 | include(FindPackageHandleStandardArgs) 198 | 199 | FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2_mixer 200 | REQUIRED_VARS SDL2_MIXER_LIBRARIES SDL2_MIXER_INCLUDE_DIRS 201 | VERSION_VAR SDL2_MIXER_VERSION_STRING) 202 | 203 | 204 | mark_as_advanced(SDL2_MIXER_PATH 205 | SDL2_MIXER_NO_DEFAULT_PATH 206 | SDL2_MIXER_LIBRARY 207 | SDL2_MIXER_INCLUDE_DIR) 208 | 209 | 210 | if(SDL2_MIXER_FOUND) 211 | 212 | # SDL2::Mixer target 213 | if(SDL2_MIXER_LIBRARY AND NOT TARGET SDL2::Mixer) 214 | add_library(SDL2::Mixer UNKNOWN IMPORTED) 215 | set_target_properties(SDL2::Mixer PROPERTIES 216 | IMPORTED_LOCATION "${SDL2_MIXER_LIBRARY}" 217 | INTERFACE_INCLUDE_DIRECTORIES "${SDL2_MIXER_INCLUDE_DIR}" 218 | INTERFACE_LINK_LIBRARIES SDL2::Core) 219 | endif() 220 | endif() 221 | -------------------------------------------------------------------------------- /cmake/sdl2/FindSDL2_net.cmake: -------------------------------------------------------------------------------- 1 | # Distributed under the OSI-approved BSD 3-Clause License. See accompanying 2 | # file Copyright.txt or https://cmake.org/licensing for details. 3 | 4 | # Copyright 2019 Amine Ben Hassouna 5 | # Copyright 2000-2019 Kitware, Inc. and Contributors 6 | # All rights reserved. 7 | 8 | # Redistribution and use in source and binary forms, with or without 9 | # modification, are permitted provided that the following conditions 10 | # are met: 11 | 12 | # * Redistributions of source code must retain the above copyright 13 | # notice, this list of conditions and the following disclaimer. 14 | 15 | # * Redistributions in binary form must reproduce the above copyright 16 | # notice, this list of conditions and the following disclaimer in the 17 | # documentation and/or other materials provided with the distribution. 18 | 19 | # * Neither the name of Kitware, Inc. nor the names of Contributors 20 | # may be used to endorse or promote products derived from this 21 | # software without specific prior written permission. 22 | 23 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | 35 | #[=======================================================================[.rst: 36 | FindSDL2_net 37 | ------------ 38 | 39 | Locate SDL2_net library 40 | 41 | This module defines the following 'IMPORTED' target: 42 | 43 | :: 44 | 45 | SDL2::Net 46 | The SDL2_net library, if found. 47 | Have SDL2::Core as a link dependency. 48 | 49 | 50 | 51 | This module will set the following variables in your project: 52 | 53 | :: 54 | 55 | SDL2_NET_LIBRARIES, the name of the library to link against 56 | SDL2_NET_INCLUDE_DIRS, where to find the headers 57 | SDL2_NET_FOUND, if false, do not try to link against 58 | SDL2_NET_VERSION_STRING - human-readable string containing the 59 | version of SDL2_net 60 | 61 | 62 | 63 | This module responds to the following cache variables: 64 | 65 | :: 66 | 67 | SDL2_NET_PATH 68 | Set a custom SDL2_net Library path (default: empty) 69 | 70 | SDL2_NET_NO_DEFAULT_PATH 71 | Disable search SDL2_net Library in default path. 72 | If SDL2_NET_PATH (default: ON) 73 | Else (default: OFF) 74 | 75 | SDL2_NET_INCLUDE_DIR 76 | SDL2_net headers path. 77 | 78 | SDL2_NET_LIBRARY 79 | SDL2_net Library (.dll, .so, .a, etc) path. 80 | 81 | 82 | Additional Note: If you see an empty SDL2_NET_LIBRARY in your project 83 | configuration, it means CMake did not find your SDL2_net library 84 | (SDL2_net.dll, libsdl2_net.so, etc). Set SDL2_NET_LIBRARY to point 85 | to your SDL2_net library, and configure again. This value is used to 86 | generate the final SDL2_NET_LIBRARIES variable and the SDL2::Net target, 87 | but when this value is unset, SDL2_NET_LIBRARIES and SDL2::Net does not 88 | get created. 89 | 90 | 91 | $SDL2NETDIR is an environment variable that would correspond to the 92 | ./configure --prefix=$SDL2NETDIR used in building SDL2_net. 93 | 94 | $SDL2DIR is an environment variable that would correspond to the 95 | ./configure --prefix=$SDL2DIR used in building SDL2. 96 | 97 | 98 | 99 | Created by Amine Ben Hassouna: 100 | Adapt FindSDL_net.cmake to SDL2_net (FindSDL2_net.cmake). 101 | Add cache variables for more flexibility: 102 | SDL2_NET_PATH, SDL2_NET_NO_DEFAULT_PATH (for details, see doc above). 103 | Add SDL2 as a required dependency. 104 | Modernize the FindSDL2_net.cmake module by creating a specific target: 105 | SDL2::Net (for details, see doc above). 106 | 107 | Original FindSDL_net.cmake module: 108 | Created by Eric Wing. This was influenced by the FindSDL.cmake 109 | module, but with modifications to recognize OS X frameworks and 110 | additional Unix paths (FreeBSD, etc). 111 | #]=======================================================================] 112 | 113 | # SDL2 Library required 114 | find_package(SDL2 QUIET) 115 | if(NOT SDL2_FOUND) 116 | set(SDL2_NET_SDL2_NOT_FOUND "Could NOT find SDL2 (SDL2 is required by SDL2_net).") 117 | if(SDL2_net_FIND_REQUIRED) 118 | message(FATAL_ERROR ${SDL2_NET_SDL2_NOT_FOUND}) 119 | else() 120 | if(NOT SDL2_net_FIND_QUIETLY) 121 | message(STATUS ${SDL2_NET_SDL2_NOT_FOUND}) 122 | endif() 123 | return() 124 | endif() 125 | unset(SDL2_NET_SDL2_NOT_FOUND) 126 | endif() 127 | 128 | 129 | # Define options for searching SDL2_net Library in a custom path 130 | 131 | set(SDL2_NET_PATH "" CACHE STRING "Custom SDL2_net Library path") 132 | 133 | set(_SDL2_NET_NO_DEFAULT_PATH OFF) 134 | if(SDL2_NET_PATH) 135 | set(_SDL2_NET_NO_DEFAULT_PATH ON) 136 | endif() 137 | 138 | set(SDL2_NET_NO_DEFAULT_PATH ${_SDL2_NET_NO_DEFAULT_PATH} 139 | CACHE BOOL "Disable search SDL2_net Library in default path") 140 | unset(_SDL2_NET_NO_DEFAULT_PATH) 141 | 142 | set(SDL2_NET_NO_DEFAULT_PATH_CMD) 143 | if(SDL2_NET_NO_DEFAULT_PATH) 144 | set(SDL2_NET_NO_DEFAULT_PATH_CMD NO_DEFAULT_PATH) 145 | endif() 146 | 147 | # Search for the SDL2_net include directory 148 | find_path(SDL2_NET_INCLUDE_DIR SDL_net.h 149 | HINTS 150 | ENV SDL2NETDIR 151 | ENV SDL2DIR 152 | ${SDL2_NET_NO_DEFAULT_PATH_CMD} 153 | PATH_SUFFIXES SDL2 154 | # path suffixes to search inside ENV{SDL2DIR} 155 | # and ENV{SDL2NETDIR} 156 | include/SDL2 include 157 | PATHS ${SDL2_NET_PATH} 158 | DOC "Where the SDL2_net headers can be found" 159 | ) 160 | 161 | if(CMAKE_SIZEOF_VOID_P EQUAL 8) 162 | set(VC_LIB_PATH_SUFFIX lib/x64) 163 | else() 164 | set(VC_LIB_PATH_SUFFIX lib/x86) 165 | endif() 166 | 167 | # Search for the SDL2_net library 168 | find_library(SDL2_NET_LIBRARY 169 | NAMES SDL2_net 170 | HINTS 171 | ENV SDL2NETDIR 172 | ENV SDL2DIR 173 | ${SDL2_NET_NO_DEFAULT_PATH_CMD} 174 | PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX} 175 | PATHS ${SDL2_NET_PATH} 176 | DOC "Where the SDL2_net Library can be found" 177 | ) 178 | 179 | # Read SDL2_net version 180 | if(SDL2_NET_INCLUDE_DIR AND EXISTS "${SDL2_NET_INCLUDE_DIR}/SDL_net.h") 181 | file(STRINGS "${SDL2_NET_INCLUDE_DIR}/SDL_net.h" SDL2_NET_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL_NET_MAJOR_VERSION[ \t]+[0-9]+$") 182 | file(STRINGS "${SDL2_NET_INCLUDE_DIR}/SDL_net.h" SDL2_NET_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL_NET_MINOR_VERSION[ \t]+[0-9]+$") 183 | file(STRINGS "${SDL2_NET_INCLUDE_DIR}/SDL_net.h" SDL2_NET_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL_NET_PATCHLEVEL[ \t]+[0-9]+$") 184 | string(REGEX REPLACE "^#define[ \t]+SDL_NET_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_NET_VERSION_MAJOR "${SDL2_NET_VERSION_MAJOR_LINE}") 185 | string(REGEX REPLACE "^#define[ \t]+SDL_NET_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_NET_VERSION_MINOR "${SDL2_NET_VERSION_MINOR_LINE}") 186 | string(REGEX REPLACE "^#define[ \t]+SDL_NET_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL2_NET_VERSION_PATCH "${SDL2_NET_VERSION_PATCH_LINE}") 187 | set(SDL2_NET_VERSION_STRING ${SDL2_NET_VERSION_MAJOR}.${SDL2_NET_VERSION_MINOR}.${SDL2_NET_VERSION_PATCH}) 188 | unset(SDL2_NET_VERSION_MAJOR_LINE) 189 | unset(SDL2_NET_VERSION_MINOR_LINE) 190 | unset(SDL2_NET_VERSION_PATCH_LINE) 191 | unset(SDL2_NET_VERSION_MAJOR) 192 | unset(SDL2_NET_VERSION_MINOR) 193 | unset(SDL2_NET_VERSION_PATCH) 194 | endif() 195 | 196 | set(SDL2_NET_LIBRARIES ${SDL2_NET_LIBRARY}) 197 | set(SDL2_NET_INCLUDE_DIRS ${SDL2_NET_INCLUDE_DIR}) 198 | 199 | include(FindPackageHandleStandardArgs) 200 | 201 | FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2_net 202 | REQUIRED_VARS SDL2_NET_LIBRARIES SDL2_NET_INCLUDE_DIRS 203 | VERSION_VAR SDL2_NET_VERSION_STRING) 204 | 205 | 206 | mark_as_advanced(SDL2_NET_PATH 207 | SDL2_NET_NO_DEFAULT_PATH 208 | SDL2_NET_LIBRARY 209 | SDL2_NET_INCLUDE_DIR) 210 | 211 | 212 | if(SDL2_NET_FOUND) 213 | 214 | # SDL2::Net target 215 | if(SDL2_NET_LIBRARY AND NOT TARGET SDL2::Net) 216 | add_library(SDL2::Net UNKNOWN IMPORTED) 217 | set_target_properties(SDL2::Net PROPERTIES 218 | IMPORTED_LOCATION "${SDL2_NET_LIBRARY}" 219 | INTERFACE_INCLUDE_DIRECTORIES "${SDL2_NET_INCLUDE_DIR}" 220 | INTERFACE_LINK_LIBRARIES SDL2::Core) 221 | endif() 222 | endif() 223 | -------------------------------------------------------------------------------- /cmake/sdl2/FindSDL2_ttf.cmake: -------------------------------------------------------------------------------- 1 | # Distributed under the OSI-approved BSD 3-Clause License. See accompanying 2 | # file Copyright.txt or https://cmake.org/licensing for details. 3 | 4 | # Copyright 2019 Amine Ben Hassouna 5 | # Copyright 2000-2019 Kitware, Inc. and Contributors 6 | # All rights reserved. 7 | 8 | # Redistribution and use in source and binary forms, with or without 9 | # modification, are permitted provided that the following conditions 10 | # are met: 11 | 12 | # * Redistributions of source code must retain the above copyright 13 | # notice, this list of conditions and the following disclaimer. 14 | 15 | # * Redistributions in binary form must reproduce the above copyright 16 | # notice, this list of conditions and the following disclaimer in the 17 | # documentation and/or other materials provided with the distribution. 18 | 19 | # * Neither the name of Kitware, Inc. nor the names of Contributors 20 | # may be used to endorse or promote products derived from this 21 | # software without specific prior written permission. 22 | 23 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 | # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 29 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 30 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 31 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 32 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 | 35 | #[=======================================================================[.rst: 36 | FindSDL2_ttf 37 | ------------ 38 | 39 | Locate SDL2_ttf library 40 | 41 | This module defines the following 'IMPORTED' target: 42 | 43 | :: 44 | 45 | SDL2::TTF 46 | The SDL2_ttf library, if found. 47 | Have SDL2::Core as a link dependency. 48 | 49 | 50 | 51 | This module will set the following variables in your project: 52 | 53 | :: 54 | 55 | SDL2_TTF_LIBRARIES, the name of the library to link against 56 | SDL2_TTF_INCLUDE_DIRS, where to find the headers 57 | SDL2_TTF_FOUND, if false, do not try to link against 58 | SDL2_TTF_VERSION_STRING - human-readable string containing the 59 | version of SDL2_ttf 60 | 61 | 62 | 63 | This module responds to the following cache variables: 64 | 65 | :: 66 | 67 | SDL2_TTF_PATH 68 | Set a custom SDL2_ttf Library path (default: empty) 69 | 70 | SDL2_TTF_NO_DEFAULT_PATH 71 | Disable search SDL2_ttf Library in default path. 72 | If SDL2_TTF_PATH (default: ON) 73 | Else (default: OFF) 74 | 75 | SDL2_TTF_INCLUDE_DIR 76 | SDL2_ttf headers path. 77 | 78 | SDL2_TTF_LIBRARY 79 | SDL2_ttf Library (.dll, .so, .a, etc) path. 80 | 81 | 82 | Additional Note: If you see an empty SDL2_TTF_LIBRARY in your project 83 | configuration, it means CMake did not find your SDL2_ttf library 84 | (SDL2_ttf.dll, libsdl2_ttf.so, etc). Set SDL2_TTF_LIBRARY to point 85 | to your SDL2_ttf library, and configure again. This value is used to 86 | generate the final SDL2_TTF_LIBRARIES variable and the SDL2::TTF target, 87 | but when this value is unset, SDL2_TTF_LIBRARIES and SDL2::TTF does not 88 | get created. 89 | 90 | 91 | $SDL2TTFDIR is an environment variable that would correspond to the 92 | ./configure --prefix=$SDL2TTFDIR used in building SDL2_ttf. 93 | 94 | $SDL2DIR is an environment variable that would correspond to the 95 | ./configure --prefix=$SDL2DIR used in building SDL2. 96 | 97 | 98 | 99 | Created by Amine Ben Hassouna: 100 | Adapt FindSDL_ttf.cmake to SDL2_ttf (FindSDL2_ttf.cmake). 101 | Add cache variables for more flexibility: 102 | SDL2_TTF_PATH, SDL2_TTF_NO_DEFAULT_PATH (for details, see doc above). 103 | Add SDL2 as a required dependency. 104 | Modernize the FindSDL2_ttf.cmake module by creating a specific target: 105 | SDL2::TTF (for details, see doc above). 106 | 107 | Original FindSDL_ttf.cmake module: 108 | Created by Eric Wing. This was influenced by the FindSDL.cmake 109 | module, but with modifications to recognize OS X frameworks and 110 | additional Unix paths (FreeBSD, etc). 111 | #]=======================================================================] 112 | 113 | # SDL2 Library required 114 | find_package(SDL2 QUIET) 115 | if(NOT SDL2_FOUND) 116 | set(SDL2_TTF_SDL2_NOT_FOUND "Could NOT find SDL2 (SDL2 is required by SDL2_ttf).") 117 | if(SDL2_ttf_FIND_REQUIRED) 118 | message(FATAL_ERROR ${SDL2_TTF_SDL2_NOT_FOUND}) 119 | else() 120 | if(NOT SDL2_ttf_FIND_QUIETLY) 121 | message(STATUS ${SDL2_TTF_SDL2_NOT_FOUND}) 122 | endif() 123 | return() 124 | endif() 125 | unset(SDL2_TTF_SDL2_NOT_FOUND) 126 | endif() 127 | 128 | 129 | # Define options for searching SDL2_ttf Library in a custom path 130 | 131 | set(SDL2_TTF_PATH "" CACHE STRING "Custom SDL2_ttf Library path") 132 | 133 | set(_SDL2_TTF_NO_DEFAULT_PATH OFF) 134 | if(SDL2_TTF_PATH) 135 | set(_SDL2_TTF_NO_DEFAULT_PATH ON) 136 | endif() 137 | 138 | set(SDL2_TTF_NO_DEFAULT_PATH ${_SDL2_TTF_NO_DEFAULT_PATH} 139 | CACHE BOOL "Disable search SDL2_ttf Library in default path") 140 | unset(_SDL2_TTF_NO_DEFAULT_PATH) 141 | 142 | set(SDL2_TTF_NO_DEFAULT_PATH_CMD) 143 | if(SDL2_TTF_NO_DEFAULT_PATH) 144 | set(SDL2_TTF_NO_DEFAULT_PATH_CMD NO_DEFAULT_PATH) 145 | endif() 146 | 147 | # Search for the SDL2_ttf include directory 148 | find_path(SDL2_TTF_INCLUDE_DIR SDL_ttf.h 149 | HINTS 150 | ENV SDL2TTFDIR 151 | ENV SDL2DIR 152 | ${SDL2_TTF_NO_DEFAULT_PATH_CMD} 153 | PATH_SUFFIXES SDL2 154 | # path suffixes to search inside ENV{SDL2DIR} 155 | # and ENV{SDL2TTFDIR} 156 | include/SDL2 include 157 | PATHS ${SDL2_TTF_PATH} 158 | DOC "Where the SDL2_ttf headers can be found" 159 | ) 160 | 161 | if(CMAKE_SIZEOF_VOID_P EQUAL 8) 162 | set(VC_LIB_PATH_SUFFIX lib/x64) 163 | else() 164 | set(VC_LIB_PATH_SUFFIX lib/x86) 165 | endif() 166 | 167 | # Search for the SDL2_ttf library 168 | find_library(SDL2_TTF_LIBRARY 169 | NAMES SDL2_ttf 170 | HINTS 171 | ENV SDL2TTFDIR 172 | ENV SDL2DIR 173 | ${SDL2_TTF_NO_DEFAULT_PATH_CMD} 174 | PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX} 175 | PATHS ${SDL2_TTF_PATH} 176 | DOC "Where the SDL2_ttf Library can be found" 177 | ) 178 | 179 | # Read SDL2_ttf version 180 | if(SDL2_TTF_INCLUDE_DIR AND EXISTS "${SDL2_TTF_INCLUDE_DIR}/SDL_ttf.h") 181 | file(STRINGS "${SDL2_TTF_INCLUDE_DIR}/SDL_ttf.h" SDL2_TTF_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL_TTF_MAJOR_VERSION[ \t]+[0-9]+$") 182 | file(STRINGS "${SDL2_TTF_INCLUDE_DIR}/SDL_ttf.h" SDL2_TTF_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL_TTF_MINOR_VERSION[ \t]+[0-9]+$") 183 | file(STRINGS "${SDL2_TTF_INCLUDE_DIR}/SDL_ttf.h" SDL2_TTF_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL_TTF_PATCHLEVEL[ \t]+[0-9]+$") 184 | string(REGEX REPLACE "^#define[ \t]+SDL_TTF_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_TTF_VERSION_MAJOR "${SDL2_TTF_VERSION_MAJOR_LINE}") 185 | string(REGEX REPLACE "^#define[ \t]+SDL_TTF_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_TTF_VERSION_MINOR "${SDL2_TTF_VERSION_MINOR_LINE}") 186 | string(REGEX REPLACE "^#define[ \t]+SDL_TTF_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL2_TTF_VERSION_PATCH "${SDL2_TTF_VERSION_PATCH_LINE}") 187 | set(SDL2_TTF_VERSION_STRING ${SDL2_TTF_VERSION_MAJOR}.${SDL2_TTF_VERSION_MINOR}.${SDL2_TTF_VERSION_PATCH}) 188 | unset(SDL2_TTF_VERSION_MAJOR_LINE) 189 | unset(SDL2_TTF_VERSION_MINOR_LINE) 190 | unset(SDL2_TTF_VERSION_PATCH_LINE) 191 | unset(SDL2_TTF_VERSION_MAJOR) 192 | unset(SDL2_TTF_VERSION_MINOR) 193 | unset(SDL2_TTF_VERSION_PATCH) 194 | endif() 195 | 196 | set(SDL2_TTF_LIBRARIES ${SDL2_TTF_LIBRARY}) 197 | set(SDL2_TTF_INCLUDE_DIRS ${SDL2_TTF_INCLUDE_DIR}) 198 | 199 | include(FindPackageHandleStandardArgs) 200 | 201 | FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2_ttf 202 | REQUIRED_VARS SDL2_TTF_LIBRARIES SDL2_TTF_INCLUDE_DIRS 203 | VERSION_VAR SDL2_TTF_VERSION_STRING) 204 | 205 | 206 | mark_as_advanced(SDL2_TTF_PATH 207 | SDL2_TTF_NO_DEFAULT_PATH 208 | SDL2_TTF_LIBRARY 209 | SDL2_TTF_INCLUDE_DIR) 210 | 211 | 212 | if(SDL2_TTF_FOUND) 213 | 214 | # SDL2::TTF target 215 | if(SDL2_TTF_LIBRARY AND NOT TARGET SDL2::TTF) 216 | add_library(SDL2::TTF UNKNOWN IMPORTED) 217 | set_target_properties(SDL2::TTF PROPERTIES 218 | IMPORTED_LOCATION "${SDL2_TTF_LIBRARY}" 219 | INTERFACE_INCLUDE_DIRECTORIES "${SDL2_TTF_INCLUDE_DIR}" 220 | INTERFACE_LINK_LIBRARIES SDL2::Core) 221 | endif() 222 | endif() 223 | -------------------------------------------------------------------------------- /cmake/sdl2/README.md: -------------------------------------------------------------------------------- 1 | # SDL2 CMake modules 2 | 3 | This repository contains [CMake][] modules for finding and using the SDL2 4 | library as well as other related libraries: 5 | 6 | - [SDL2][] 7 | - [SDL2_image][] 8 | - [SDL2_ttf][] 9 | - [SDL2_net][] 10 | - [SDL2_mixer][] 11 | - [SDL2_gfx][] 12 | 13 | These modules are based on the SDL (1.2) modules, with the same names, 14 | distributed with the CMake project. The SDL2_gfx module is also based 15 | on the SDL_image module. 16 | 17 | ## Details and Improvements 18 | 19 | The improvements made to these modules are as follows: 20 | 21 | **FindSDL2.cmake** 22 | 23 | - Adapt `FindSDL.cmake` to `SDL2` (`FindSDL2.cmake`). 24 | - Add cache variables for more flexibility:
25 | `SDL2_PATH`, `SDL2_NO_DEFAULT_PATH` 26 | - Mark `Threads` as a required dependency for non-OSX systems. 27 | - Modernize the `FindSDL2.cmake` module by creating specific targets: 28 | - `SDL2::Core` : Library project should link to `SDL2::Core` 29 | - `SDL2::Main` : Application project should link to `SDL2::Main` 30 | 31 | *For more details, please see the embedded documentation in `FindSDL2.cmake` file.* 32 | 33 | **FindSDL2_<COMPONENT>.cmake** 34 | 35 | - Adapt `FindSDL_.cmake` to `SDL2_` (`FindSDL2_.cmake`). 36 | - Add cache variables for more flexibility:
37 | `SDL2__PATH`, `SDL2__NO_DEFAULT_PATH` 38 | - Add `SDL2` as a required dependency. 39 | - Modernize the `FindSDL2_.cmake` modules by creating specific targets:
40 | `SDL2::Image`, `SDL2::TTF`, `SDL2::Net`, `SDL2::Mixer` and `SDL2::GFX`. 41 | 42 | *For more details, please see the embedded documentation in 43 | `FindSDL2_.cmake` file.* 44 | 45 | ## Usage 46 | 47 | In order to use the SDL2 CMake modules, we have to clone this repository in a 48 | sud-directory `cmake/sdl2` in our project as follows: 49 | 50 | ```sh 51 | cd 52 | git clone https://gitlab.com/aminosbh/sdl2-cmake-modules.git cmake/sdl2 53 | rm -rf cmake/sdl2/.git 54 | ``` 55 | 56 | Or if we are using git for our project, we can add this repository as a 57 | submodule as follows: 58 | 59 | ```sh 60 | cd 61 | git submodule add https://gitlab.com/aminosbh/sdl2-cmake-modules.git cmake/sdl2 62 | git commit -m "Add SDL2 CMake modules" 63 | ``` 64 | 65 | Then we should specify the modules path in the main CMakeLists.txt file like 66 | the following: 67 | 68 | ```cmake 69 | list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/sdl2) 70 | ``` 71 | 72 | Finally, we can use the SDL2 modules. There is two approaches that can be 73 | adopted: A legacy approach and a modern approach. Both of them are supported. 74 | 75 | ### Modern CMake 76 | 77 | We can link to the SDL2:: targets like the following example:
78 | *This example requires the SDL2, SDL2_image and the SDL2_gfx libraries* 79 | 80 | ```cmake 81 | # Find SDL2, SDL2_image and SDL2_gfx libraries 82 | find_package(SDL2 REQUIRED) 83 | find_package(SDL2_image REQUIRED) 84 | find_package(SDL2_gfx REQUIRED) 85 | 86 | # Link SDL2::Main, SDL2::Image and SDL2::GFX to our project 87 | target_link_libraries(${PROJECT_NAME} SDL2::Main SDL2::Image SDL2::GFX) 88 | ``` 89 | 90 | *Use the appropriate packages for you project.*
91 | *Please see above, for the whole list of packages*
92 | *For more details, please see the embedded documentation in modules files* 93 | 94 | ### Legacy CMake 95 | 96 | We can also specify manually the include directories and libraries to link to: 97 | 98 | ```cmake 99 | # Find and link SDL2 100 | find_package(SDL2 REQUIRED) 101 | target_include_directories(${PROJECT_NAME} PRIVATE ${SDL2_INCLUDE_DIRS}) 102 | target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES}) 103 | 104 | # Find and link SDL2_image 105 | find_package(SDL2_image REQUIRED) 106 | target_include_directories(${PROJECT_NAME} PRIVATE ${SDL2_IMAGE_INCLUDE_DIRS}) 107 | target_link_libraries(${PROJECT_NAME} ${SDL2_IMAGE_LIBRARIES}) 108 | 109 | # Find and link SDL2_gfx 110 | find_package(SDL2_gfx REQUIRED) 111 | target_include_directories(${PROJECT_NAME} PRIVATE ${SDL2_GFX_INCLUDE_DIRS}) 112 | target_link_libraries(${PROJECT_NAME} ${SDL2_GFX_LIBRARIES}) 113 | 114 | ``` 115 | 116 | *For more details, please see the embedded documentation in modules files* 117 | 118 | ## Special customization variables 119 | 120 | Each module have special customization cache variables that can be used to help 121 | the modules find the appropriate libraries: 122 | 123 | - `SDL2_PATH` and `SDL2__PATH`:
124 | Can be specified to set the root search path for the `SDL2` and `SDL2_` 125 | - `SDL2_NO_DEFAULT_PATH` and `SDL2__NO_DEFAULT_PATH`:
126 | Disable search `SDL2/SDL2_` library in default path:
127 | If `SDL2[_]_PATH` is set, defaults to ON
128 | Else defaults to OFF 129 | - `SDL2_INCLUDE_DIR` and `SDL2__INCLUDE_DIR`:
130 | Set headers path. (Override) 131 | - `SDL2_LIBRARY` and `SDL2__LIBRARY`:
132 | Set the library (.dll, .so, .a, etc) path. (Override) 133 | - `SDL2MAIN_LIBRAY`:
134 | Set the `SDL2main` library (.a) path. (Override) 135 | 136 | These variables could be used in case of Windows projects, and when the 137 | libraries are not localized in a standard pathes. They can be specified when 138 | executing the `cmake` command or when using the [CMake GUI][] (They are marked 139 | as advanced). 140 | 141 | **cmake command example:** 142 | 143 | ```sh 144 | mkdir build 145 | cd build 146 | cmake .. -DSDL2_PATH="/path/to/sdl2" 147 | ``` 148 | 149 | **CMakeLists.txt example:** 150 | 151 | If we embed, for example, binaries of the SDL2_ttf in our project, we can 152 | specify the cache variables values just before calling the `find_package` 153 | command as follows: 154 | 155 | ```cmake 156 | set(SDL2_TTF_PATH "/path/to/sdl2_ttf" CACHE BOOL "" FORCE) 157 | find_package(SDL2_ttf REQUIRED) 158 | ``` 159 | 160 | ## License 161 | 162 | Maintainer: Amine B. Hassouna [@aminosbh](https://gitlab.com/aminosbh) 163 | 164 | The SDL2 CMake modules are based on the SDL (1.2) modules available with the 165 | CMake project which is distributed under the OSI-approved BSD 3-Clause License. 166 | 167 | The SDL2 CMake modules are also distributed under the OSI-approved BSD 168 | 3-Clause License. See accompanying file [Copyright.txt](Copyright.txt). 169 | 170 | 171 | 172 | [CMake]: https://cmake.org 173 | [CMake GUI]: https://cmake.org/runningcmake 174 | [SDL2]: https://www.libsdl.org 175 | [SDL2_image]: https://www.libsdl.org/projects/SDL_image 176 | [SDL2_ttf]: https://www.libsdl.org/projects/SDL_ttf 177 | [SDL2_net]: https://www.libsdl.org/projects/SDL_net 178 | [SDL2_mixer]: https://www.libsdl.org/projects/SDL_mixer 179 | [SDL2_gfx]: http://www.ferzkopp.net/wordpress/2016/01/02/sdl_gfx-sdl2_gfx 180 | -------------------------------------------------------------------------------- /images/animation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aminosbh/sdl2-pointerless/eccb0e537ab2f06ea45445d8f092cd846e0130ac/images/animation.png -------------------------------------------------------------------------------- /images/blue-line.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aminosbh/sdl2-pointerless/eccb0e537ab2f06ea45445d8f092cd846e0130ac/images/blue-line.png -------------------------------------------------------------------------------- /images/calculator.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aminosbh/sdl2-pointerless/eccb0e537ab2f06ea45445d8f092cd846e0130ac/images/calculator.png -------------------------------------------------------------------------------- /images/display-text.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aminosbh/sdl2-pointerless/eccb0e537ab2f06ea45445d8f092cd846e0130ac/images/display-text.png -------------------------------------------------------------------------------- /images/hello-world.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aminosbh/sdl2-pointerless/eccb0e537ab2f06ea45445d8f092cd846e0130ac/images/hello-world.png -------------------------------------------------------------------------------- /images/mouse-hover.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aminosbh/sdl2-pointerless/eccb0e537ab2f06ea45445d8f092cd846e0130ac/images/mouse-hover.png -------------------------------------------------------------------------------- /images/paint.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aminosbh/sdl2-pointerless/eccb0e537ab2f06ea45445d8f092cd846e0130ac/images/paint.png -------------------------------------------------------------------------------- /images/red-dot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aminosbh/sdl2-pointerless/eccb0e537ab2f06ea45445d8f092cd846e0130ac/images/red-dot.png -------------------------------------------------------------------------------- /images/text-editor.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aminosbh/sdl2-pointerless/eccb0e537ab2f06ea45445d8f092cd846e0130ac/images/text-editor.png -------------------------------------------------------------------------------- /include/.include_dir: -------------------------------------------------------------------------------- 1 | This is the include directory 2 | -------------------------------------------------------------------------------- /rename_project.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # 4 | # Copyright (c) 2019 Amine Ben Hassouna 5 | # 6 | # Permission is hereby granted, free of charge, to any 7 | # person obtaining a copy of this software and associated 8 | # documentation files (the "Software"), to deal in the 9 | # Software without restriction, including without 10 | # limitation the rights to use, copy, modify, merge, 11 | # publish, distribute, sublicense, and/or sell copies of 12 | # the Software, and to permit persons to whom the Software 13 | # is furnished to do so, subject to the following 14 | # conditions: 15 | # 16 | # The above copyright notice and this permission notice 17 | # shall be included in all copies or substantial portions 18 | # of the Software. 19 | # 20 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 21 | # ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 22 | # TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 23 | # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 24 | # SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 25 | # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 26 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 27 | # IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 28 | # DEALINGS IN THE SOFTWARE. 29 | # 30 | 31 | 32 | # ---------------------------- 33 | # Script to rename the project 34 | # ---------------------------- 35 | 36 | # Autodetect the git repository 37 | PROJECT_GIT_REPO_AUTO=$(git remote get-url origin 2> /dev/null) 38 | if echo "${PROJECT_GIT_REPO_AUTO}" | grep -o "^git@.*:" > /dev/null 2>&1; then 39 | # Convert to https 40 | PROJECT_GIT_REPO_AUTO_=$(echo "${PROJECT_GIT_REPO_AUTO}" | cut -d'@' -f 2) 41 | PROJECT_GIT_REPO_AUTO_HOST=$(echo "${PROJECT_GIT_REPO_AUTO_}" | cut -d':' -f 1) 42 | PROJECT_GIT_REPO_AUTO_PATH=$(echo "${PROJECT_GIT_REPO_AUTO_}" | cut -d':' -f 2) 43 | PROJECT_GIT_REPO_AUTO="https://${PROJECT_GIT_REPO_AUTO_HOST}/${PROJECT_GIT_REPO_AUTO_PATH}" 44 | fi 45 | 46 | # Specify the git repository 47 | echo "Please enter the git repository path (No semicolons):" 48 | echo -e "Press enter to keep the autodetected git repository:\n\e[32m${PROJECT_GIT_REPO_AUTO}\e[0m" 49 | read PROJECT_GIT_REPO 50 | if [ -z "$PROJECT_GIT_REPO" ]; then 51 | PROJECT_GIT_REPO="${PROJECT_GIT_REPO_AUTO}" 52 | fi 53 | echo 54 | 55 | 56 | # Autodetect the project and executable name 57 | PROJECT_EXECUTABLE_NAME_AUTO="$(echo "${PROJECT_GIT_REPO}" | rev | cut -d'/' -f 1 | rev | cut -d'.' -f 1)" 58 | PROJECT_NAME_AUTO=$(echo "${PROJECT_EXECUTABLE_NAME_AUTO}" | sed 's/-/ /g') 59 | PROJECT_NAME_AUTO="${PROJECT_NAME_AUTO^}" 60 | 61 | # Specify the project name 62 | echo "Please enter the project name (No slashes '/'):" 63 | echo -e "Press enter to keep the autodetected project name: \e[32m'${PROJECT_NAME_AUTO}'\e[0m" 64 | read PROJECT_NAME 65 | if [ -z "$PROJECT_NAME" ]; then 66 | PROJECT_NAME="${PROJECT_NAME_AUTO}" 67 | fi 68 | echo 69 | 70 | # Specify the executable name 71 | echo "Please enter the project executable name (No spaces, no slashes '/')" 72 | echo -e "Press enter to keep the autodetected executable name: \e[32m'${PROJECT_EXECUTABLE_NAME_AUTO}'\e[0m" 73 | read PROJECT_EXECUTABLE_NAME 74 | if [ -z "$PROJECT_EXECUTABLE_NAME" ]; then 75 | PROJECT_EXECUTABLE_NAME="${PROJECT_EXECUTABLE_NAME_AUTO}" 76 | fi 77 | echo 78 | 79 | 80 | # Display a summary for confirmation 81 | echo 82 | echo "This script will rename your project, like the following:" 83 | echo -e "Project git repository: \e[32m'${PROJECT_GIT_REPO}'\e[0m" 84 | echo -e "Project name: \e[32m'${PROJECT_NAME}'\e[0m" 85 | echo -e "Project executable name: \e[32m'${PROJECT_EXECUTABLE_NAME}'\e[0m" 86 | echo 87 | echo -e "\e[31mDo you confirm your modifications ? [y/N]\e[0m" 88 | read RENAME_PROJECT 89 | if [ -n "$RENAME_PROJECT" ] && [ "$(echo "$RENAME_PROJECT" | tr a-z A-Z)" = "Y" ]; then 90 | sed -i "s/SDL2 pointerless basic project/${PROJECT_NAME}/g" README.md rename_project.sh 91 | sed -i "s/sdl2-pointerless/${PROJECT_EXECUTABLE_NAME}/g" README.md IDE_USAGE.md CMakeLists.txt rename_project.sh 92 | sed -i "s;https://gitlab.com/aminosbh/sdl2-pointerless.git;${PROJECT_GIT_REPO};g" README.md rename_project.sh 93 | echo -e "\e[32mThe project was successfully renamed\e[0m" 94 | else 95 | echo -e "\e[33mOperation aborted\e[0m" 96 | fi 97 | 98 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 |  2 | // Define screen dimensions 3 | #define SCREEN_WIDTH (800) 4 | #define SCREEN_HEIGHT (600) 5 | 6 | // Max grid dimension 7 | #define GRID_MAX_X_CELLS (10) 8 | #define GRID_MAX_Y_CELLS (10) 9 | 10 | #define GRID_DEFAULT_MARGIN (20) 11 | #define GRID_DEFAULT_COLOR COLOR_WHITE 12 | #define GRID_DEFAULT_BORDER_SIZE (2) 13 | #define GRID_DEFAULT_BORDER_COLOR COLOR_GRAY 14 | 15 | #define USE_AZERTY_KEYBOARD 0 16 | 17 | //*************************************************************************************** 18 | // IMPORTANT NOTE: 19 | // 20 | // To start coding, go down in this file to the 'FREE CODING' section. 21 | // All your code should be written inside the 'draw' function (line 1725). 22 | // There is also a 'DOCUMENTATION' section just above the 'FREE CODING' section. 23 | // 24 | // Read the README.md file for more details. 25 | // 26 | //*************************************************************************************** 27 | 28 | /* 29 | * Copyright (c) 2018, 2019, 2021 Amine Ben Hassouna 30 | * All rights reserved. 31 | * 32 | * Permission is hereby granted, free of charge, to any 33 | * person obtaining a copy of this software and associated 34 | * documentation files (the "Software"), to deal in the 35 | * Software without restriction, including without 36 | * limitation the rights to use, copy, modify, merge, 37 | * publish, distribute, sublicense, and/or sell copies of 38 | * the Software, and to permit persons to whom the Software 39 | * is furnished to do so, subject to the following 40 | * conditions: 41 | * 42 | * The above copyright notice and this permission notice 43 | * shall be included in all copies or substantial portions 44 | * of the Software. 45 | * 46 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 47 | * ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 48 | * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 49 | * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 50 | * SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 51 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 52 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 53 | * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 54 | * DEALINGS IN THE SOFTWARE. 55 | * 56 | * --------------------------------------------------------------------------------- 57 | * The 4x6 font: 58 | * - Upper case letters 59 | * - Lower case letters 60 | * - Exclamation mark 61 | * - Piriod (punctuation) 62 | * 63 | * Belongs to https://github.com/filmote 64 | * Source: https://github.com/filmote/Font4x6/blob/master/src/fonts/Font4x6.cpp 65 | * License: https://github.com/filmote/Font4x6/blob/master/LICENSE 66 | 67 | BSD 3-Clause License 68 | 69 | Copyright (c) 2018, Filmote 70 | All rights reserved. 71 | 72 | Redistribution and use in source and binary forms, with or without 73 | modification, are permitted provided that the following conditions are met: 74 | 75 | * Redistributions of source code must retain the above copyright notice, this 76 | list of conditions and the following disclaimer. 77 | 78 | * Redistributions in binary form must reproduce the above copyright notice, 79 | this list of conditions and the following disclaimer in the documentation 80 | and/or other materials provided with the distribution. 81 | 82 | * Neither the name of the copyright holder nor the names of its 83 | contributors may be used to endorse or promote products derived from 84 | this software without specific prior written permission. 85 | 86 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 87 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 88 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 89 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 90 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 91 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 92 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 93 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 94 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 95 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 96 | * --------------------------------------------------------------------------------- 97 | * 98 | * The 4x6 font modification/extension: 99 | * Some lower case letters have been modifed and the remaining font characters are made 100 | * by "Amine Ben Hassouna" and are distributed under the same license as this file. 101 | * 102 | */ 103 | 104 | //**************************************************************************************** 105 | // LIBRARY SECTION 106 | //**************************************************************************************** 107 | 108 | #include 109 | #include 110 | #include 111 | #include 112 | 113 | // Define MAX and MIN macros 114 | #define max(X, Y) (((X) > (Y)) ? (X) : (Y)) 115 | #define min(X, Y) (((X) < (Y)) ? (X) : (Y)) 116 | 117 | #define _COLOR(RED, GREEN, BLUE) { RED, GREEN, BLUE, 0xFF } 118 | #define COLOR(RED, GREEN, BLUE) ((SDL_Color) { RED, GREEN, BLUE, 0xFF }) 119 | 120 | const SDL_Color NO_COLOR = {0, 0, 0, 0}; 121 | const SDL_Color COLOR_BLACK = _COLOR(0, 0, 0); 122 | const SDL_Color COLOR_WHITE = _COLOR(0xFF, 0xFF, 0xFF); 123 | const SDL_Color COLOR_GRAY = _COLOR(0x64, 0x64, 0x64); 124 | const SDL_Color COLOR_DARK_GRAY = _COLOR(0x1E, 0x1E, 0x1E); 125 | const SDL_Color COLOR_LIGHT_GRAY = _COLOR(0xC8, 0xC8, 0xC8); 126 | const SDL_Color COLOR_RED = _COLOR(0xF5, 0x3B, 0x56); 127 | const SDL_Color COLOR_GREEN = _COLOR(0x01, 0x9F, 0x13); 128 | const SDL_Color COLOR_BLUE = _COLOR(0x38, 0x95, 0xD3); 129 | const SDL_Color COLOR_YELLOW = _COLOR(0xF7, 0xDC, 0x11); 130 | const SDL_Color COLOR_ORANGE = _COLOR(0xFF, 0x85, 0); 131 | const SDL_Color COLOR_PINK = _COLOR(0xFF, 0, 0xCE); 132 | const SDL_Color COLOR_VIOLET = _COLOR(0x91, 0, 0xFF); 133 | 134 | enum 135 | { 136 | SDLK_DIVIDE = SDLK_SLASH, 137 | SDLK_MULTIPLY = SDLK_ASTERISK, 138 | }; 139 | 140 | SDL_Keycode normalize_key(SDL_Keycode key) 141 | { 142 | switch (key) 143 | { 144 | case SDLK_KP_0: 145 | return SDLK_0; 146 | case SDLK_KP_1: 147 | return SDLK_1; 148 | case SDLK_KP_2: 149 | return SDLK_2; 150 | case SDLK_KP_3: 151 | return SDLK_3; 152 | case SDLK_KP_4: 153 | return SDLK_4; 154 | case SDLK_KP_5: 155 | return SDLK_5; 156 | case SDLK_KP_6: 157 | return SDLK_6; 158 | case SDLK_KP_7: 159 | return SDLK_7; 160 | case SDLK_KP_8: 161 | return SDLK_8; 162 | case SDLK_KP_9: 163 | return SDLK_9; 164 | case SDLK_KP_ENTER: 165 | return SDLK_RETURN; 166 | case SDLK_KP_PERIOD: 167 | return SDLK_PERIOD; 168 | case SDLK_KP_COMMA: 169 | return SDLK_COMMA; 170 | case SDLK_KP_COLON: 171 | return SDLK_COLON; 172 | case SDLK_KP_PERCENT: 173 | return SDLK_PERCENT; 174 | #if USE_AZERTY_KEYBOARD 175 | case 249: 176 | return SDLK_PERCENT; 177 | #endif 178 | case SDLK_KP_PLUS: 179 | return SDLK_PLUS; 180 | case SDLK_KP_MINUS: 181 | return SDLK_MINUS; 182 | case SDLK_KP_MULTIPLY: 183 | return SDLK_MULTIPLY; 184 | case SDLK_KP_DIVIDE: 185 | return SDLK_DIVIDE; 186 | case SDLK_KP_EQUALS: 187 | return SDLK_EQUALS; 188 | default: 189 | break; 190 | } 191 | 192 | return key; 193 | } 194 | 195 | bool is_key_ascii(SDL_Keycode key) 196 | { 197 | return key >= SDLK_SPACE && key <= SDLK_z; 198 | } 199 | 200 | bool is_key_letter(SDL_Keycode key) 201 | { 202 | return key >= SDLK_a && key <= SDLK_z; 203 | } 204 | 205 | bool is_key_digit(SDL_Keycode key) 206 | { 207 | return key >= SDLK_0 && key <= SDLK_9; 208 | } 209 | 210 | bool is_key_arithmetic_op(SDL_Keycode key) 211 | { 212 | return key == SDLK_PLUS 213 | || key == SDLK_MINUS 214 | || key == SDLK_DIVIDE 215 | || key == SDLK_MULTIPLY 216 | || key == SDLK_PERCENT; 217 | } 218 | 219 | char key_to_char(SDL_Keycode key) 220 | { 221 | key = normalize_key(key); 222 | 223 | if (is_key_letter(key)) 224 | { 225 | return (char) (key - SDLK_a + 'A'); 226 | } 227 | else if (key >= SDLK_SPACE && key <= SDLK_BACKQUOTE) 228 | { 229 | return (char) key; 230 | } 231 | 232 | return 0; 233 | } 234 | 235 | char key_to_char_lowercase(SDL_Keycode key) 236 | { 237 | if (is_key_letter(key)) 238 | { 239 | return (char) key; 240 | } 241 | else 242 | { 243 | return key_to_char(key); 244 | } 245 | } 246 | 247 | 248 | int key_to_int(SDL_Keycode key) 249 | { 250 | key = normalize_key(key); 251 | 252 | if (is_key_digit(key)) 253 | { 254 | return (int) (key - SDLK_0); 255 | } 256 | else 257 | { 258 | return -1; 259 | } 260 | } 261 | 262 | SDL_Keycode digit_to_key(int digit) 263 | { 264 | return (abs(digit) % 10) + SDLK_0; 265 | } 266 | 267 | char digit_to_char(int digit) 268 | { 269 | return (abs(digit) % 10) + '0'; 270 | } 271 | 272 | int char_to_digit(char c) 273 | { 274 | return c - '0'; 275 | } 276 | 277 | struct Cell 278 | { 279 | // Rect dimensions and color 280 | SDL_Rect rect; 281 | SDL_Color rect_color; 282 | 283 | // Border dimensions and color 284 | SDL_Rect border; 285 | SDL_Color border_color; 286 | }; 287 | typedef struct Cell Cell; 288 | 289 | 290 | struct Grid 291 | { 292 | // x, y, width, height 293 | SDL_Rect rect; 294 | 295 | // Grid background color 296 | SDL_Color background_color; 297 | 298 | // Grid border thickness and color 299 | unsigned int border; 300 | SDL_Color border_color; 301 | 302 | // Number of cells over the x axis 303 | int x_cells; 304 | // Number of cells over the y axis 305 | int y_cells; 306 | 307 | // Cells boder thickness and color 308 | unsigned int cells_border; 309 | SDL_Color cells_border_color; 310 | 311 | // Matrix of Cells 312 | Cell cells[GRID_MAX_X_CELLS][GRID_MAX_Y_CELLS]; 313 | }; 314 | typedef struct Grid Grid; 315 | 316 | void set_background_color(SDL_Renderer* renderer, SDL_Color color) 317 | { 318 | // Initialize renderer color 319 | SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a); 320 | 321 | // Clear screen 322 | SDL_RenderClear(renderer); 323 | } 324 | 325 | int random_int(int min, int max) 326 | { 327 | return ( rand() % (max + 1) ) + min; 328 | } 329 | 330 | bool is_color_equal(SDL_Color color1, SDL_Color color2) 331 | { 332 | return *((Sint32*) &color1) == *((Sint32*) &color2); 333 | } 334 | 335 | void init_cell(Grid* grid, Cell* cell, int i, int j, SDL_Color color, SDL_Color border_color) 336 | { 337 | // Init rect 338 | int interspace_width = grid->x_cells * grid->cells_border * 2; 339 | cell->rect.w = (grid->rect.w - (grid->border * 2) - interspace_width) / grid->x_cells; 340 | 341 | int interspace_heigth = grid->y_cells * grid->cells_border * 2; 342 | cell->rect.h = (grid->rect.h - (grid->border * 2) - interspace_heigth) / grid->y_cells; 343 | 344 | cell->rect.x = grid->rect.x + grid->border + grid->cells_border 345 | + (grid->cells_border * 2 + cell->rect.w) * i; 346 | cell->rect.y = grid->rect.y + grid->border + grid->cells_border 347 | + (grid->cells_border * 2 + cell->rect.h) * j; 348 | 349 | // Init rectColor 350 | cell->rect_color = color; 351 | 352 | // Init border 353 | cell->border.w = cell->rect.w + grid->cells_border * 2; 354 | cell->border.h = cell->rect.h + grid->cells_border * 2; 355 | cell->border.x = cell->rect.x - grid->cells_border; 356 | cell->border.y = cell->rect.y - grid->cells_border; 357 | 358 | // Init borderColor 359 | cell->border_color = border_color; 360 | } 361 | 362 | bool init_grid(Grid* grid) 363 | { 364 | if(!grid->rect.w || !grid->rect.h || !grid->x_cells || !grid->y_cells) 365 | { 366 | fprintf(stderr, "Grid dimensions or number of cells not initialised !\n"); 367 | return false; 368 | } 369 | 370 | if(grid->x_cells > GRID_MAX_X_CELLS || grid->y_cells > GRID_MAX_Y_CELLS) 371 | { 372 | fprintf(stderr, "Grid number of cells (%d,%d) is greater than (%d,%d) !\n", 373 | grid->x_cells, grid->y_cells, 374 | GRID_MAX_X_CELLS, GRID_MAX_Y_CELLS); 375 | return false; 376 | } 377 | 378 | // Init all cells 379 | for(int i = 0; i < grid->x_cells; ++i) 380 | { 381 | for(int j = 0; j < grid->y_cells; ++j) 382 | { 383 | init_cell(grid, 384 | &(grid->cells[i][j]), 385 | i, j, 386 | grid->background_color, 387 | grid->cells_border_color); 388 | } 389 | } 390 | 391 | return true; 392 | } 393 | 394 | 395 | void render_cell(Cell* cell, SDL_Renderer* renderer) 396 | { 397 | if(cell->border.x != cell->rect.x) // Cells border thickness different from 0 398 | { 399 | // Set renderer color to cell color 400 | SDL_SetRenderDrawColor(renderer, 401 | cell->border_color.r, 402 | cell->border_color.g, 403 | cell->border_color.b, 404 | cell->border_color.a); 405 | 406 | // Render filled cell 407 | SDL_RenderFillRect(renderer, &(cell->border)); 408 | } 409 | 410 | // Set renderer color to cell color 411 | SDL_SetRenderDrawColor(renderer, 412 | cell->rect_color.r, 413 | cell->rect_color.g, 414 | cell->rect_color.b, 415 | cell->rect_color.a); 416 | 417 | // Render filled cell 418 | SDL_RenderFillRect(renderer, &(cell->rect)); 419 | } 420 | 421 | void render_grid(Grid* grid, SDL_Renderer* renderer) 422 | { 423 | if(grid->border != 0) // Grid border thickness different from 0 424 | { 425 | // Set renderer color to draw the grid border 426 | SDL_SetRenderDrawColor(renderer, 427 | grid->border_color.r, 428 | grid->border_color.g, 429 | grid->border_color.b, 430 | grid->border_color.a); 431 | 432 | // Render grid border 433 | SDL_RenderFillRect(renderer, &(grid->rect)); 434 | } 435 | 436 | // Render all cells 437 | for(int i = 0; i < grid->x_cells; ++i) 438 | { 439 | for(int j = 0; j < grid->y_cells; ++j) 440 | { 441 | render_cell(&(grid->cells[i][j]), renderer); 442 | } 443 | } 444 | } 445 | 446 | int ajust_grid_size(Grid* grid) 447 | { 448 | if(!grid->rect.w || !grid->rect.h || !grid->x_cells || !grid->y_cells) 449 | { 450 | fprintf(stderr, "Grid dimensions or number of cells not initialised !\n"); 451 | return false; 452 | } 453 | 454 | // Init rect 455 | int interspace_width = grid->x_cells * grid->cells_border * 2; 456 | grid->rect.w -= (grid->rect.w - (grid->border * 2) - interspace_width) % grid->x_cells; 457 | 458 | int interspace_heigth = grid->y_cells * grid->cells_border * 2; 459 | grid->rect.h -= (grid->rect.h - (grid->border * 2) - interspace_heigth) % grid->y_cells; 460 | 461 | return true; 462 | } 463 | 464 | void center_grid(Grid* grid, int screen_width, int screen_height) 465 | { 466 | grid->rect.x = (screen_width - grid->rect.w) / 2; 467 | grid->rect.y = (screen_height - grid->rect.h) / 2; 468 | } 469 | 470 | void set_cell_color(Grid* grid, int x, int y, SDL_Color color) 471 | { 472 | if (x >= 0 && x < grid->x_cells && y >= 0 && y < grid->y_cells) 473 | { 474 | grid->cells[x][y].rect_color = color; 475 | } 476 | } 477 | 478 | SDL_Color get_cell_color(Grid* grid, int x, int y) 479 | { 480 | if (x >= 0 && x < grid->x_cells && y >= 0 && y < grid->y_cells) 481 | { 482 | return grid->cells[x][y].rect_color; 483 | } 484 | 485 | return NO_COLOR; 486 | } 487 | 488 | void set_cell_border_color(Grid* grid, int x, int y, SDL_Color color) 489 | { 490 | if (x >= 0 && x < grid->x_cells && y >= 0 && y < grid->y_cells) 491 | { 492 | grid->cells[x][y].border_color = color; 493 | } 494 | } 495 | 496 | SDL_Color get_cell_border_color(Grid* grid, int x, int y) 497 | { 498 | if (x >= 0 && x < grid->x_cells && y >= 0 && y < grid->y_cells) 499 | { 500 | return grid->cells[x][y].border_color; 501 | } 502 | 503 | return NO_COLOR; 504 | } 505 | 506 | void set_grid_color(Grid* grid, SDL_Color color) 507 | { 508 | grid->background_color = color; 509 | 510 | // Set color of all cells 511 | for(int x = 0; x < grid->x_cells; ++x) 512 | { 513 | for(int y = 0; y < grid->y_cells; ++y) 514 | { 515 | grid->cells[x][y].rect_color = color; 516 | } 517 | } 518 | } 519 | 520 | SDL_Color get_grid_color(Grid* grid) 521 | { 522 | return grid->background_color; 523 | } 524 | 525 | void set_grid_border_color(Grid* grid, SDL_Color color) 526 | { 527 | grid->border_color = color; 528 | grid->cells_border_color = color; 529 | 530 | // Set border color of all cells 531 | for(int x = 0; x < grid->x_cells; ++x) 532 | { 533 | for(int y = 0; y < grid->y_cells; ++y) 534 | { 535 | grid->cells[x][y].border_color = color; 536 | } 537 | } 538 | } 539 | 540 | SDL_Color get_grid_border_color(Grid* grid) 541 | { 542 | return grid->border_color; 543 | } 544 | 545 | #define FONT_PIXELS_UNKNOWN 1 546 | #define FONT_PIXELS_NON_PRINTABLE_CHAR 1 547 | #define FONT_PIXELS_PRINTABLE_CHAR 5 548 | 549 | const uint8_t FONT_PIXELS[] = 550 | { 551 | 0, 552 | 553 | // Unknown / Non printable 554 | 0x3F, // ░░▓▓▓▓▓▓ 555 | 0x31, // ░░▓▓░░░▓ 556 | 0x31, // ░░▓▓░░░▓ 557 | 0x3F, // ░░▓▓▓▓▓▓ 558 | 559 | 560 | // Sign list 1 561 | 562 | // #32 Symbol ' ' 563 | 0x00, // ░░░░░░░░ 564 | 0x00, // ░░░░░░░░ 565 | 0x00, // ░░░░░░░░ 566 | 0x00, // ░░░░░░░░ 567 | 568 | // #33 Symbol '!' 569 | 0x00, // ░░░░░░░░ 570 | 0x2F, // ░░▓░▓▓▓▓ 571 | 0x00, // ░░░░░░░░ 572 | 0x00, // ░░░░░░░░ 573 | 574 | // #34 Symbol " 575 | 0x03, // ░░░░░░▓▓ 576 | 0x00, // ░░░░░░░░ 577 | 0x03, // ░░░░░░▓▓ 578 | 0x00, // ░░░░░░░░ 579 | 580 | // #35 Symbol '#' 581 | 0x0A, // ░░░░▓░▓░ 582 | 0x1F, // ░░░▓▓▓▓▓ 583 | 0x0A, // ░░░░▓░▓░ 584 | 0x1F, // ░░░▓▓▓▓▓ 585 | 586 | // #36 Symbol '$' 587 | 0x16, // ░░░▓░▓▓░ 588 | 0x3F, // ░░▓▓▓▓▓▓ 589 | 0x1A, // ░░░▓▓░▓░ 590 | 0x00, // ░░░░░░░░ 591 | 592 | // #37 Symbol '%' 593 | 0x12, // ░░░▓░░▓░ 594 | 0x08, // ░░░░▓░░░ 595 | 0x04, // ░░░░░▓░░ 596 | 0x12, // ░░░▓░░▓░ 597 | 598 | // #38 Symbol '&' 599 | 0x37, // ░░▓▓░▓▓▓ 600 | 0x29, // ░░▓░▓░░▓ 601 | 0x3D, // ░░▓▓▓▓░▓ 602 | 0x08, // ░░░░▓░░░ 603 | 604 | // #39 Symbol ' 605 | 0x00, // ░░░░░░░░ 606 | 0x03, // ░░░░░░▓▓ 607 | 0x00, // ░░░░░░░░ 608 | 0x00, // ░░░░░░░░ 609 | 610 | // #40 Symbol '(' 611 | 0x00, // ░░░░░░░░ 612 | 0x1E, // ░░░▓▓▓▓░ 613 | 0x21, // ░░▓░░░░▓ 614 | 0x00, // ░░░░░░░░ 615 | 616 | // #41 Symbol ')' 617 | 0x00, // ░░░░░░░░ 618 | 0x21, // ░░▓░░░░▓ 619 | 0x1E, // ░░░▓▓▓▓░ 620 | 0x00, // ░░░░░░░░ 621 | 622 | // #42 Symbol '*' 623 | 0x0A, // ░░░░▓░▓░ 624 | 0x04, // ░░░░░▓░░ 625 | 0x0A, // ░░░░▓░▓░ 626 | 0x00, // ░░░░░░░░ 627 | 628 | // #43 Symbol '+' 629 | 0x08, // ░░░░▓░░░ 630 | 0x1C, // ░░░▓▓▓░░ 631 | 0x08, // ░░░░▓░░░ 632 | 0x00, // ░░░░░░░░ 633 | 634 | // #44 Symbol ',' 635 | 0x00, // ░░░░░░░░ 636 | 0x20, // ░░▓░░░░░ 637 | 0x10, // ░░░▓░░░░ 638 | 0x00, // ░░░░░░░░ 639 | 640 | // #45 Symbol '-' 641 | 0x08, // ░░░░▓░░░ 642 | 0x08, // ░░░░▓░░░ 643 | 0x08, // ░░░░▓░░░ 644 | 0x00, // ░░░░░░░░ 645 | 646 | // #46 Symbol '.' 647 | 0x00, // ░░░░░░░░ 648 | 0x20, // ░░▓░░░░░ 649 | 0x00, // ░░░░░░░░ 650 | 0x00, // ░░░░░░░░ 651 | 652 | // #47 Symbol '/' 653 | 0x10, // ░░░▓░░░░ 654 | 0x08, // ░░░░▓░░░ 655 | 0x04, // ░░░░░▓░░ 656 | 0x02, // ░░░░░░▓░ 657 | 658 | 659 | // Digits 660 | 661 | // #48 Number '0' 662 | 0x1E, // ░░░▓▓▓▓░ 663 | 0x29, // ░░▓░▓░░▓ 664 | 0x25, // ░░▓░░▓░▓ 665 | 0x1E, // ░░░▓▓▓▓░ 666 | 667 | // #49 Number '1' 668 | 0x22, // ░░▓░░░▓░ 669 | 0x3F, // ░░▓▓▓▓▓▓ 670 | 0x20, // ░░▓░░░░░ 671 | 0x00, // ░░░░░░░░ 672 | 673 | // #50 Number '2' 674 | 0x32, // ░░▓▓░░▓░ 675 | 0x29, // ░░▓░▓░░▓ 676 | 0x29, // ░░▓░▓░░▓ 677 | 0x26, // ░░▓░░▓▓░ 678 | 679 | // #51 Number '3' 680 | 0x12, // ░░░▓░░▓░ 681 | 0x21, // ░░▓░░░░▓ 682 | 0x25, // ░░▓░░▓░▓ 683 | 0x1A, // ░░░▓▓░▓░ 684 | 685 | // #52 Number '4' 686 | 0x0C, // ░░░░▓▓░░ 687 | 0x0A, // ░░░░▓░▓░ 688 | 0x3F, // ░░▓▓▓▓▓▓ 689 | 0x08, // ░░░░▓░░░ 690 | 691 | // #53 Number '5' 692 | 0x17, // ░░░▓░▓▓▓ 693 | 0x25, // ░░▓░░▓░▓ 694 | 0x25, // ░░▓░░▓░▓ 695 | 0x19, // ░░░▓▓░░▓ 696 | 697 | // #54 Number '6' 698 | 0x1E, // ░░░▓▓▓▓░ 699 | 0x25, // ░░▓░░▓░▓ 700 | 0x25, // ░░▓░░▓░▓ 701 | 0x18, // ░░░▓▓░░░ 702 | 703 | // #55 Number '7'. 704 | 0x01, // ░░░░░░░▓ 705 | 0x39, // ░░▓▓▓░░▓ 706 | 0x05, // ░░░░░▓░▓ 707 | 0x03, // ░░░░░░▓▓ 708 | 709 | // #56 Number '8' 710 | 0x1A, // ░░░▓▓░▓░ 711 | 0x25, // ░░▓░░▓░▓ 712 | 0x25, // ░░▓░░▓░▓ 713 | 0x1A, // ░░░▓▓░▓░ 714 | 715 | // #57 Number '9' 716 | 0x06, // ░░░░░▓▓░ 717 | 0x29, // ░░▓░▓░░▓ 718 | 0x29, // ░░▓░▓░░▓ 719 | 0x1E, // ░░░▓▓▓▓░ 720 | 721 | 722 | // Sign list 2 723 | 724 | // #58 Symbol ':' 725 | 0x00, // ░░░░░░░░ 726 | 0x14, // ░░░▓░▓░░ 727 | 0x00, // ░░░░░░░░ 728 | 0x00, // ░░░░░░░░ 729 | 730 | // #59 Symbol ';' 731 | 0x00, // ░░░░░░░░ 732 | 0x20, // ░░▓░░░░░ 733 | 0x14, // ░░░▓░▓░░ 734 | 0x00, // ░░░░░░░░ 735 | 736 | // #60 Symbol '<' 737 | 0x08, // ░░░░▓░░░ 738 | 0x14, // ░░░▓░▓░░ 739 | 0x22, // ░░▓░░░▓░ 740 | 0x00, // ░░░░░░░░ 741 | 742 | // #61 Symbol '=' 743 | 0x14, // ░░░▓░▓░░ 744 | 0x14, // ░░░▓░▓░░ 745 | 0x14, // ░░░▓░▓░░ 746 | 0x00, // ░░░░░░░░ 747 | 748 | // #62 Symbol '>' 749 | 0x22, // ░░▓░░░▓░ 750 | 0x14, // ░░░▓░▓░░ 751 | 0x08, // ░░░░▓░░░ 752 | 0x00, // ░░░░░░░░ 753 | 754 | // #63 Symbol '?' 755 | 0x00, // ░░░░░░░░ 756 | 0x29, // ░░▓░▓░░▓ 757 | 0x06, // ░░░░░▓▓░ 758 | 0x00, // ░░░░░░░░ 759 | 760 | // #64 Symbol '@' 761 | 0x3F, // ░░▓▓▓▓▓▓ 762 | 0x21, // ░░▓░░░░▓ 763 | 0x2D, // ░░▓░▓▓░▓ 764 | 0x0F, // ░░░░▓▓▓▓ 765 | 766 | 767 | // Upper case letters 768 | 769 | // #65 Letter 'A' 770 | 0x3E, // ░░▓▓▓▓▓░ 771 | 0x09, // ░░░░▓░░▓ 772 | 0x09, // ░░░░▓░░▓ 773 | 0x3E, // ░░▓▓▓▓▓░ 774 | 775 | // #66 Letter 'B' 776 | 0x3F, // ░░▓▓▓▓▓▓ 777 | 0x25, // ░░▓░░▓░▓ 778 | 0x25, // ░░▓░░▓░▓ 779 | 0x1A, // ░░░▓▓░▓░ 780 | 781 | // #67 Letter 'C' 782 | 0x1E, // ░░░▓▓▓▓░ 783 | 0x21, // ░░▓░░░░▓ 784 | 0x21, // ░░▓░░░░▓ 785 | 0x12, // ░░░▓░░▓░ 786 | 787 | // #68 Letter 'D' 788 | 0x3F, // ░░▓▓▓▓▓▓ 789 | 0x21, // ░░▓░░░░▓ 790 | 0x21, // ░░▓░░░░▓ 791 | 0x1E, // ░░░▓▓▓▓░ 792 | 793 | // #69 Letter 'E' 794 | 0x3F, // ░░▓▓▓▓▓▓ 795 | 0x25, // ░░▓░░▓░▓ 796 | 0x25, // ░░▓░░▓░▓ 797 | 0x21, // ░░▓░░░░▓ 798 | 799 | // #70 Letter 'F' 800 | 0x3F, // ░░▓▓▓▓▓▓ 801 | 0x05, // ░░░░░▓░▓ 802 | 0x05, // ░░░░░▓░▓ 803 | 0x01, // ░░░░░░░▓ 804 | 805 | // #71 Letter 'G' 806 | 0x1E, // ░░░▓▓▓▓░ 807 | 0x21, // ░░▓░░░░▓ 808 | 0x29, // ░░▓░▓░░▓ 809 | 0x3A, // ░░▓▓▓░▓░ 810 | 811 | // #72 Letter 'H' 812 | 0x3F, // ░░▓▓▓▓▓▓ 813 | 0x04, // ░░░░░▓░░ 814 | 0x04, // ░░░░░▓░░ 815 | 0x3F, // ░░▓▓▓▓▓▓ 816 | 817 | // #73 Letter 'I' 818 | 0x21, // ░░▓░░░░▓ 819 | 0x3F, // ░░▓▓▓▓▓▓ 820 | 0x21, // ░░▓░░░░▓ 821 | 0x00, // ░░░░░░░░ 822 | 823 | // #74 Letter 'J' 824 | 0x10, // ░░░▓░░░░ 825 | 0x21, // ░░▓░░░░▓ 826 | 0x21, // ░░▓░░░░▓ 827 | 0x1F, // ░░░▓▓▓▓▓ 828 | 829 | // #75 Letter 'K' 830 | 0x3F, // ░░▓▓▓▓▓▓ 831 | 0x04, // ░░░░░▓░░ 832 | 0x0A, // ░░░░▓░▓░ 833 | 0x31, // ░░▓▓░░░▓ 834 | 835 | // #76 Letter 'L' 836 | 0x3F, // ░░▓▓▓▓▓▓ 837 | 0x20, // ░░▓░░░░░ 838 | 0x20, // ░░▓░░░░░ 839 | 0x20, // ░░▓░░░░░ 840 | 841 | // #77 Letter 'M' 842 | 0x3F, // ░░▓▓▓▓▓▓ 843 | 0x02, // ░░░░░░▓░ 844 | 0x02, // ░░░░░░▓░ 845 | 0x3F, // ░░▓▓▓▓▓▓ 846 | 847 | // #78 Letter 'N' 848 | 0x3F, // ░░▓▓▓▓▓▓ 849 | 0x02, // ░░░░░░▓░ 850 | 0x04, // ░░░░░▓░░ 851 | 0x3F, // ░░▓▓▓▓▓▓ 852 | 853 | // #79 Letter 'O' 854 | 0x1E, // ░░░▓▓▓▓░ 855 | 0x21, // ░░▓░░░░▓ 856 | 0x21, // ░░▓░░░░▓ 857 | 0x1E, // ░░░▓▓▓▓░ 858 | 859 | // #80 Letter 'P' 860 | 0x3F, // ░░▓▓▓▓▓▓ 861 | 0x09, // ░░░░▓░░▓ 862 | 0x09, // ░░░░▓░░▓ 863 | 0x06, // ░░░░░▓▓░ 864 | 865 | // #81 Letter 'Q' 866 | 0x1E, // ░░░▓▓▓▓░ 867 | 0x21, // ░░▓░░░░▓ 868 | 0x11, // ░░░▓░░░▓ 869 | 0x2E, // ░░▓░▓▓▓░ 870 | 871 | // #82 Letter 'R' 872 | 0x3F, // ░░▓▓▓▓▓▓ 873 | 0x09, // ░░░░▓░░▓ 874 | 0x09, // ░░░░▓░░▓ 875 | 0x36, // ░░▓▓░▓▓░ 876 | 877 | // #83 Letter 'S' 878 | 0x22, // ░░▓░░░▓░ 879 | 0x25, // ░░▓░░▓░▓ 880 | 0x25, // ░░▓░░▓░▓ 881 | 0x19, // ░░░▓▓░░▓ 882 | 883 | // #84 Letter 'T' 884 | 0x01, // ░░░░░░░▓ 885 | 0x3F, // ░░▓▓▓▓▓▓ 886 | 0x01, // ░░░░░░░▓ 887 | 0x01, // ░░░░░░░▓ 888 | 889 | // #85 Letter 'U' 890 | 0x1F, // ░░░▓▓▓▓▓ 891 | 0x20, // ░░▓░░░░░ 892 | 0x20, // ░░▓░░░░░ 893 | 0x1F, // ░░░▓▓▓▓▓ 894 | 895 | // #86 Letter 'V' 896 | 0x0F, // ░░░░▓▓▓▓ 897 | 0x10, // ░░░▓░░░░ 898 | 0x20, // ░░▓░░░░░ 899 | 0x1F, // ░░░▓▓▓▓▓ 900 | 901 | // #87 Letter 'W' 902 | 0x3F, // ░░▓▓▓▓▓▓ 903 | 0x10, // ░░░▓░░░░ 904 | 0x10, // ░░░▓░░░░ 905 | 0x3F, // ░░▓▓▓▓▓▓ 906 | 907 | // #88 Letter 'X' 908 | 0x3B, // ░░▓▓▓░▓▓ 909 | 0x04, // ░░░░░▓░░ 910 | 0x04, // ░░░░░▓░░ 911 | 0x3B, // ░░▓▓▓░▓▓ 912 | 913 | // #89 Letter 'Y' 914 | 0x03, // ░░░░░░▓▓ 915 | 0x04, // ░░░░░▓░░ 916 | 0x38, // ░░▓▓▓░░░ 917 | 0x07, // ░░░░░▓▓▓ 918 | 919 | // #90 Letter 'Z' 920 | 0x31, // ░░▓▓░░░▓ 921 | 0x2D, // ░░▓░▓▓░▓ 922 | 0x23, // ░░▓░░░▓▓ 923 | 0x21, // ░░▓░░░░▓ 924 | 925 | 926 | // Sign list 3 927 | 928 | // #91 Symbol '[' 929 | 0x00, // ░░░░░░░░ 930 | 0x3F, // ░░▓▓▓▓▓▓ 931 | 0x21, // ░░▓░░░░▓ 932 | 0x00, // ░░░░░░░░ 933 | 934 | // #92 Symbol '\' 935 | 0x02, // ░░░░░░▓░ 936 | 0x04, // ░░░░░▓░░ 937 | 0x08, // ░░░░▓░░░ 938 | 0x10, // ░░░▓░░░░ 939 | 940 | // #93 Symbol ']' 941 | 0x00, // ░░░░░░░░ 942 | 0x21, // ░░▓░░░░▓ 943 | 0x3F, // ░░▓▓▓▓▓▓ 944 | 0x00, // ░░░░░░░░ 945 | 946 | // #94 Symbol '^' 947 | 0x02, // ░░░░░░▓░ 948 | 0x01, // ░░░░░░░▓ 949 | 0x02, // ░░░░░░▓░ 950 | 0x00, // ░░░░░░░░ 951 | 952 | // #95 Symbol '_' 953 | 0x20, // ░░▓░░░░░ 954 | 0x20, // ░░▓░░░░░ 955 | 0x20, // ░░▓░░░░░ 956 | 0x20, // ░░▓░░░░░ 957 | 958 | // #96 Symbol '`' 959 | 0x00, // ░░░░░░░░ 960 | 0x01, // ░░░░░░░▓ 961 | 0x02, // ░░░░░░▓░ 962 | 0x00, // ░░░░░░░░ 963 | 964 | 965 | // Lower case letters 966 | 967 | // #97 Letter 'a' 968 | 0x10, // ░░░▓░░░░ 969 | 0x2A, // ░░▓░▓░▓░ 970 | 0x2A, // ░░▓░▓░▓░ 971 | 0x3C, // ░░▓▓▓▓░░ 972 | 973 | // #98 Letter 'b' 974 | 0x3F, // ░░▓▓▓▓▓▓ 975 | 0x24, // ░░▓░░▓░░ 976 | 0x24, // ░░▓░░▓░░ 977 | 0x18, // ░░░▓▓░░░ 978 | 979 | // #99 Letter 'c' 980 | 0x1C, // ░░░▓▓▓░░ 981 | 0x22, // ░░▓░░░▓░ 982 | 0x22, // ░░▓░░░▓░ 983 | 0x14, // ░░░▓░▓░░ 984 | 985 | // #100 Letter 'd' 986 | 0x18, // ░░░▓▓░░░ 987 | 0x24, // ░░▓░░▓░░ 988 | 0x24, // ░░▓░░▓░░ 989 | 0x3F, // ░░▓▓▓▓▓▓ 990 | 991 | // #101 Letter 'e' 992 | 0x1C, // ░░░▓▓▓░░ 993 | 0x2A, // ░░▓░▓░▓░ 994 | 0x2A, // ░░▓░▓░▓░ 995 | 0x2C, // ░░▓░▓▓░░ 996 | 997 | // #102 Letter 'f' 998 | 0x04, // ░░░░░▓░░ 999 | 0x3E, // ░░▓▓▓▓▓░ 1000 | 0x05, // ░░░░░▓░▓ 1001 | 0x01, // ░░░░░░░▓ 1002 | 1003 | // #103 Letter 'g' 1004 | 0x26, // ░░▓░░▓▓░ 1005 | 0x29, // ░░▓░▓░░▓ 1006 | 0x29, // ░░▓░▓░░▓ 1007 | 0x1E, // ░░░▓▓▓▓▓ 1008 | 1009 | // #104 Letter 'h' 1010 | 0x3F, // ░░▓▓▓▓▓▓ 1011 | 0x04, // ░░░░░▓░░ 1012 | 0x04, // ░░░░░▓░░ 1013 | 0x38, // ░░▓▓▓░░░ 1014 | 1015 | // #105 Letter 'i' 1016 | 0x24, // ░░▓░░▓░░ 1017 | 0x3D, // ░░▓▓▓▓░▓ 1018 | 0x20, // ░░▓░░░░░ 1019 | 0x00, // ░░░░░░░░ 1020 | 1021 | // #106 Letter 'j' 1022 | 0x00, // ░░░░░░░░ 1023 | 0x20, // ░░▓░░░░░ 1024 | 0x24, // ░░▓░░▓░░ 1025 | 0x1D, // ░░░▓▓▓░▓ 1026 | 1027 | // #107 Letter 'k' 1028 | 0x3F, // ░░▓▓▓▓▓▓ 1029 | 0x08, // ░░░░▓░░░ 1030 | 0x14, // ░░░▓░▓░░ 1031 | 0x22, // ░░▓░░░▓░ 1032 | 1033 | // #108 Letter 'l' 1034 | 0x21, // ░░▓░░░░▓ 1035 | 0x3F, // ░░▓▓▓▓▓▓ 1036 | 0x20, // ░░▓░░░░░ 1037 | 0x00, // ░░░░░░░░ 1038 | 1039 | // #109 Letter 'm' 1040 | 0x3E, // ░░▓▓▓▓▓░ 1041 | 0x04, // ░░░░░▓░░ 1042 | 0x04, // ░░░░░▓░░ 1043 | 0x3E, // ░░▓▓▓▓▓░ 1044 | 1045 | // #110 Letter 'n' 1046 | 0x3E, // ░░▓▓▓▓▓░ 1047 | 0x04, // ░░░░░▓░░ 1048 | 0x02, // ░░░░░░▓░ 1049 | 0x3C, // ░░▓▓▓▓░░ 1050 | 1051 | // #111 Letter 'o' 1052 | 0x1C, // ░░░▓▓▓░░ 1053 | 0x22, // ░░▓░░░▓░ 1054 | 0x22, // ░░▓░░░▓░ 1055 | 0x1C, // ░░░▓▓▓░░ 1056 | 1057 | // #112 Letter 'p' 1058 | 0x3E, // ░░▓▓▓▓▓░ 1059 | 0x12, // ░░░▓░░▓░ 1060 | 0x12, // ░░░▓░░▓░ 1061 | 0x0C, // ░░░░▓▓░░ 1062 | 1063 | // #113 Letter 'q' 1064 | 0x0C, // ░░░░▓▓░░ 1065 | 0x12, // ░░░▓░░▓░ 1066 | 0x12, // ░░░▓░░▓░ 1067 | 0x3E, // ░░▓▓▓▓▓░ 1068 | 1069 | // #114 Letter 'r' 1070 | 0x3E, // ░░▓▓▓▓▓░ 1071 | 0x04, // ░░░░░▓░░ 1072 | 0x02, // ░░░░░░▓░ 1073 | 0x04, // ░░░░░▓░░ 1074 | 1075 | // #115 Letter 's' 1076 | 0x24, // ░░▓░░▓░░ 1077 | 0x2A, // ░░▓░▓░▓░ 1078 | 0x2A, // ░░▓░▓░▓░ 1079 | 0x12, // ░░░▓░░▓░ 1080 | 1081 | // #116 Letter 't' 1082 | 0x02, // ░░░░░░▓░ 1083 | 0x1F, // ░░░▓▓▓▓▓ 1084 | 0x22, // ░░▓░░░▓░ 1085 | 0x20, // ░░▓░░░░░ 1086 | 1087 | // #117 Letter 'u' 1088 | 0x1E, // ░░░▓▓▓▓░ 1089 | 0x20, // ░░▓░░░░░ 1090 | 0x20, // ░░▓░░░░░ 1091 | 0x1E, // ░░░▓▓▓▓░ 1092 | 1093 | // #118 Letter 'v' 1094 | 0x0E, // ░░░░▓▓▓░ 1095 | 0x10, // ░░░▓░░░░ 1096 | 0x20, // ░░▓░░░░░ 1097 | 0x1E, // ░░░▓▓▓▓░ 1098 | 1099 | // #119 Letter 'w' 1100 | 0x3E, // ░░▓▓▓▓▓░ 1101 | 0x10, // ░░░▓░░░░ 1102 | 0x10, // ░░░▓░░░░ 1103 | 0x3E, // ░░▓▓▓▓▓░ 1104 | 1105 | // #120 Letter 'x' 1106 | 0x36, // ░░▓▓░▓▓░ 1107 | 0x08, // ░░░░▓░░░ 1108 | 0x08, // ░░░░▓░░░ 1109 | 0x36, // ░░▓▓░▓▓░ 1110 | 1111 | // #121 Letter 'y' 1112 | 0x26, // ░░▓░░▓▓░ 1113 | 0x28, // ░░▓░▓░░░ 1114 | 0x28, // ░░▓░▓░░░ 1115 | 0x1E, // ░░░▓▓▓▓░ 1116 | 1117 | // #122 Letter 'z' 1118 | 0x32, // ░░▓▓░░▓░ 1119 | 0x2A, // ░░▓░▓░▓░ 1120 | 0x26, // ░░▓░░▓▓░ 1121 | 0x22, // ░░▓░░░▓░ 1122 | 1123 | 1124 | // Sign list 4 1125 | 1126 | // #123 Symbol '{' 1127 | 0x08, // ░░░░▓░░░ 1128 | 0x3F, // ░░▓▓▓▓▓▓ 1129 | 0x21, // ░░▓░░░░▓ 1130 | 0x00, // ░░░░░░░░ 1131 | 1132 | // #124 Symbol '|' 1133 | 0x00, // ░░░░░░░░ 1134 | 0x3F, // ░░▓▓▓▓▓▓ 1135 | 0x00, // ░░░░░░░░ 1136 | 0x00, // ░░░░░░░░ 1137 | 1138 | // #125 Symbol '}' 1139 | 0x00, // ░░░░░░░░ 1140 | 0x21, // ░░▓░░░░▓ 1141 | 0x3F, // ░░▓▓▓▓▓▓ 1142 | 0x08, // ░░░░▓░░░ 1143 | 1144 | // #126 Symbol '~' 1145 | 0x10, // ░░░▓░░░░ 1146 | 0x08, // ░░░░▓░░░ 1147 | 0x10, // ░░░▓░░░░ 1148 | 0x08, // ░░░░▓░░░ 1149 | }; 1150 | 1151 | void draw_font(Grid* grid, size_t font_index, int at_x, int at_y, SDL_Color color) 1152 | { 1153 | for (int x = 0; x < 4 && at_x + x < grid->x_cells; ++x) 1154 | { 1155 | const size_t k = font_index + x; 1156 | for (int y = 0; y < 6 && at_y + y < grid->y_cells; ++y) 1157 | { 1158 | set_cell_color( 1159 | grid, 1160 | at_x + x, 1161 | at_y + y, 1162 | (FONT_PIXELS[k] & (1 << y)) ? color : grid->background_color 1163 | ); 1164 | } 1165 | } 1166 | } 1167 | 1168 | void draw_char(Grid* grid, char c, int at_x, int at_y, SDL_Color color) 1169 | { 1170 | size_t font_index = FONT_PIXELS_UNKNOWN; 1171 | 1172 | if (c >= ' ' && c <= '~') 1173 | { 1174 | font_index = ((c - ' ') * 4) + FONT_PIXELS_PRINTABLE_CHAR; 1175 | } 1176 | 1177 | draw_font(grid, font_index, at_x, at_y, color); 1178 | } 1179 | 1180 | void draw_text(Grid* grid, char* text, int at_x, int at_y, int space, SDL_Color color) 1181 | { 1182 | int cursor_x = at_x; 1183 | int cursor_y = at_y; 1184 | 1185 | int i = 0; 1186 | while(text[i] && cursor_y < grid->y_cells) 1187 | { 1188 | draw_char(grid, text[i], cursor_x, cursor_y, color); 1189 | cursor_x += 4 + space; 1190 | i++; 1191 | 1192 | // Word wrap 1193 | if (cursor_x > grid->x_cells - 4) 1194 | { 1195 | cursor_x = at_x; 1196 | cursor_y += 6 + space; 1197 | 1198 | // Skip space 1199 | if (text[i] == ' ') 1200 | i++; 1201 | } 1202 | } 1203 | } 1204 | 1205 | void draw_key(Grid* grid, SDL_Keycode key, int at_x, int at_y, SDL_Color color) 1206 | { 1207 | char c = key_to_char(key); 1208 | 1209 | draw_char(grid, c, at_x, at_y, color); 1210 | } 1211 | 1212 | SDL_Keycode get_key(SDL_Event* event) 1213 | { 1214 | if (event->type == SDL_KEYDOWN) 1215 | { 1216 | return event->key.keysym.sym; 1217 | } 1218 | 1219 | return SDLK_UNKNOWN; 1220 | } 1221 | 1222 | bool _is_mouse_over_grid(Grid* grid, int x, int y) 1223 | { 1224 | return x >= grid->rect.x && x <= grid->rect.x + grid->rect.w 1225 | && y >= grid->rect.y && y <= grid->rect.y + grid->rect.h; 1226 | } 1227 | 1228 | bool is_mouse_over_grid(Grid* grid) 1229 | { 1230 | int x, y; 1231 | SDL_GetMouseState(&x, &y); 1232 | 1233 | return _is_mouse_over_grid(grid, x, y); 1234 | } 1235 | 1236 | bool is_mouse_clicked(Grid* grid, SDL_Event* event) 1237 | { 1238 | if (event->type == SDL_MOUSEBUTTONDOWN) 1239 | { 1240 | int x = event->button.x; 1241 | int y = event->button.y; 1242 | 1243 | return _is_mouse_over_grid(grid, x, y); 1244 | } 1245 | 1246 | return false; 1247 | } 1248 | 1249 | bool is_mouse_moved(Grid* grid, SDL_Event* event) 1250 | { 1251 | if (event->type == SDL_MOUSEMOTION) 1252 | { 1253 | int x = event->motion.x; 1254 | int y = event->motion.y; 1255 | 1256 | return _is_mouse_over_grid(grid, x, y); 1257 | } 1258 | 1259 | return false; 1260 | } 1261 | 1262 | int get_mouse_pos_x(Grid* grid, SDL_Event* event) 1263 | { 1264 | if (is_mouse_clicked(grid, event)) 1265 | { 1266 | int x = event->button.x; 1267 | return (x - grid->rect.x) / (grid->rect.w / grid->x_cells); 1268 | } 1269 | else if(is_mouse_moved(grid, event)) 1270 | { 1271 | int x = event->motion.x; 1272 | return (x - grid->rect.x) / (grid->rect.w / grid->x_cells); 1273 | } 1274 | else 1275 | { 1276 | int x, y; 1277 | SDL_GetMouseState(&x, &y); 1278 | 1279 | if(_is_mouse_over_grid(grid, x, y)) 1280 | { 1281 | return (x - grid->rect.x) / (grid->rect.w / grid->x_cells); 1282 | } 1283 | } 1284 | 1285 | return -1; 1286 | } 1287 | 1288 | int get_mouse_pos_y(Grid* grid, SDL_Event* event) 1289 | { 1290 | if (is_mouse_clicked(grid, event)) 1291 | { 1292 | int y = event->button.y; 1293 | return (y - grid->rect.y) / (grid->rect.h / grid->y_cells); 1294 | } 1295 | else if(is_mouse_moved(grid, event)) 1296 | { 1297 | int y = event->motion.y; 1298 | return (y - grid->rect.y) / (grid->rect.h / grid->y_cells); 1299 | } 1300 | else 1301 | { 1302 | int x, y; 1303 | SDL_GetMouseState(&x, &y); 1304 | 1305 | if(_is_mouse_over_grid(grid, x, y)) 1306 | { 1307 | return (y - grid->rect.y) / (grid->rect.h / grid->y_cells); 1308 | } 1309 | } 1310 | 1311 | return -1; 1312 | } 1313 | 1314 | Uint8 get_mouse_button(Grid* grid, SDL_Event* event) 1315 | { 1316 | if (is_mouse_clicked(grid, event)) 1317 | { 1318 | return event->button.button; 1319 | } 1320 | 1321 | return 0; 1322 | } 1323 | 1324 | bool start(SDL_Renderer* renderer, int width, int height); 1325 | 1326 | int main(int argc, char* argv[]) 1327 | { 1328 | // Unused argc, argv 1329 | (void) argc; 1330 | (void) argv; 1331 | 1332 | // Initialize the pseudo-random number generator 1333 | srand(time(NULL)); 1334 | 1335 | // Initialize SDL 1336 | if(SDL_Init(SDL_INIT_VIDEO) < 0) 1337 | { 1338 | fprintf(stderr, "SDL could not be initialized!\n" 1339 | "SDL_Error: %s\n", SDL_GetError()); 1340 | return 0; 1341 | } 1342 | 1343 | #if defined linux && SDL_VERSION_ATLEAST(2, 0, 8) 1344 | // Disable compositor bypass 1345 | if(!SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0")) 1346 | { 1347 | fprintf(stderr, "SDL can not disable compositor bypass!\n"); 1348 | return 0; 1349 | } 1350 | #endif 1351 | 1352 | // Create window 1353 | SDL_Window* window = SDL_CreateWindow("Simple grid with C and SDL2", 1354 | SDL_WINDOWPOS_UNDEFINED, 1355 | SDL_WINDOWPOS_UNDEFINED, 1356 | SCREEN_WIDTH, SCREEN_HEIGHT, 1357 | SDL_WINDOW_SHOWN); 1358 | if(!window) 1359 | { 1360 | fprintf(stderr, "Window could not be created!\n" 1361 | "SDL_Error: %s\n", SDL_GetError()); 1362 | } 1363 | else 1364 | { 1365 | // Create renderer 1366 | SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); 1367 | if(!renderer) 1368 | { 1369 | fprintf(stderr, "Renderer could not be created!\n" 1370 | "SDL_Error: %s\n", SDL_GetError()); 1371 | } 1372 | else 1373 | { 1374 | // Start rendering 1375 | start(renderer, SCREEN_WIDTH, SCREEN_HEIGHT); 1376 | 1377 | // Destroy renderer 1378 | SDL_DestroyRenderer(renderer); 1379 | } 1380 | 1381 | // Destroy window 1382 | SDL_DestroyWindow(window); 1383 | } 1384 | 1385 | // Quit SDL 1386 | SDL_Quit(); 1387 | 1388 | return 0; 1389 | } 1390 | 1391 | void delay(SDL_Renderer* renderer, Uint32 ms, SDL_Event* event) 1392 | { 1393 | event->type = 0; 1394 | 1395 | Uint32 start = SDL_GetTicks(); 1396 | int step = ms >= 10 ? 10 : 1; 1397 | while (SDL_GetTicks() - start < ms) 1398 | { 1399 | // Update screen 1400 | SDL_RenderPresent(renderer); 1401 | 1402 | SDL_Event e; 1403 | while(SDL_WaitEventTimeout(&e, step)) 1404 | { 1405 | // User requests quit 1406 | if(e.type == SDL_QUIT) 1407 | { 1408 | exit(0); 1409 | break; 1410 | } 1411 | else if(e.type == SDL_KEYDOWN) 1412 | { 1413 | *event = e; 1414 | event->key.keysym.sym = normalize_key(event->key.keysym.sym); 1415 | } 1416 | else if(e.type == SDL_MOUSEBUTTONDOWN) 1417 | { 1418 | *event = e; 1419 | } 1420 | else if(e.type == SDL_MOUSEMOTION) 1421 | { 1422 | *event = e; 1423 | } 1424 | } 1425 | } 1426 | } 1427 | 1428 | bool render_and_delay(SDL_Renderer* renderer, Grid* grid, SDL_Color background_color, Uint32 ms, SDL_Event* event) 1429 | { 1430 | // Set white background 1431 | set_background_color(renderer, background_color); 1432 | 1433 | // Render grid 1434 | render_grid(grid, renderer); 1435 | 1436 | // Wait 1437 | delay(renderer, ms, event); 1438 | 1439 | return true; 1440 | } 1441 | 1442 | // Global variables 1443 | SDL_Renderer* g_renderer = NULL; 1444 | Grid g_grid = {0}; 1445 | SDL_Color g_background_color = COLOR_WHITE; 1446 | SDL_Event g_event = {0}; 1447 | 1448 | void draw(int x_cells, int y_cells); 1449 | 1450 | bool start(SDL_Renderer* renderer, int width, int height) 1451 | { 1452 | // Init global renderer 1453 | g_renderer = renderer; 1454 | 1455 | // Set number of cells 1456 | g_grid.x_cells = GRID_MAX_X_CELLS; 1457 | g_grid.y_cells = GRID_MAX_Y_CELLS; 1458 | 1459 | // Set grid dimensions 1460 | int margin = GRID_DEFAULT_MARGIN; 1461 | int cell_size = min( (width - margin * 2) / g_grid.x_cells, 1462 | (height - margin * 2) / g_grid.y_cells ); 1463 | g_grid.rect.w = cell_size * g_grid.x_cells; 1464 | g_grid.rect.h = cell_size * g_grid.y_cells; 1465 | 1466 | // Set grid backgroud 1467 | g_grid.background_color = GRID_DEFAULT_COLOR; 1468 | 1469 | // Set grid border thickness and color 1470 | g_grid.border = GRID_DEFAULT_BORDER_SIZE; 1471 | g_grid.border_color = GRID_DEFAULT_BORDER_COLOR; 1472 | 1473 | // Set cells border thickness and color 1474 | g_grid.cells_border = g_grid.border; 1475 | g_grid.cells_border_color = g_grid.border_color; 1476 | 1477 | // Ajust size and center 1478 | ajust_grid_size(&g_grid); 1479 | center_grid(&g_grid, width, height); 1480 | 1481 | if(!init_grid(&g_grid)) 1482 | { 1483 | fprintf(stderr, "Grid fail to initialize !\n"); 1484 | return false; 1485 | } 1486 | 1487 | // Set white background 1488 | set_background_color(renderer, g_background_color); 1489 | g_grid.background_color = g_background_color; 1490 | 1491 | // Render grid 1492 | render_grid(&g_grid, renderer); 1493 | 1494 | // Update screen 1495 | SDL_RenderPresent(renderer); 1496 | 1497 | // Draw shapes 1498 | draw(g_grid.x_cells, g_grid.y_cells); 1499 | 1500 | // Set white background 1501 | set_background_color(renderer, g_background_color); 1502 | g_grid.background_color = g_background_color; 1503 | 1504 | // Render grid 1505 | render_grid(&g_grid, renderer); 1506 | 1507 | // Update screen 1508 | SDL_RenderPresent(renderer); 1509 | 1510 | // Event loop exit flag 1511 | bool quit = false; 1512 | 1513 | // Event loop 1514 | while(!quit) 1515 | { 1516 | SDL_Event e; 1517 | 1518 | // Wait indefinitely for the next available event 1519 | SDL_WaitEvent(&e); 1520 | 1521 | // User requests quit 1522 | if(e.type == SDL_QUIT) 1523 | { 1524 | quit = true; 1525 | } 1526 | else if(e.type == SDL_KEYDOWN) 1527 | { 1528 | if (e.key.keysym.sym == SDLK_ESCAPE) 1529 | { 1530 | quit = true; 1531 | } 1532 | } 1533 | 1534 | SDL_RenderPresent(renderer); 1535 | } 1536 | 1537 | return true; 1538 | } 1539 | 1540 | #define set_background_color(color) (g_background_color = color) 1541 | #define get_background_color() (g_background_color) 1542 | #define set_cell_color(x, y, color) set_cell_color(&g_grid, x, y, color) 1543 | #define get_cell_color(x, y) get_cell_color(&g_grid, x, y) 1544 | #define set_cell_border_color(x, y, color) set_cell_border_color(&g_grid, x, y, color) 1545 | #define get_cell_border_color(x, y) get_cell_border_color(&g_grid, x, y) 1546 | #define set_grid_color(color) set_grid_color(&g_grid, color) 1547 | #define get_grid_color() get_grid_color(&g_grid) 1548 | #define set_grid_border_color(color) set_grid_border_color(&g_grid, color) 1549 | #define get_grid_border_color() get_grid_border_color(&g_grid) 1550 | #define delay(ms) render_and_delay(g_renderer, &g_grid, g_background_color, ms, &g_event) 1551 | #define get_key() get_key(&g_event) 1552 | #define is_mouse_over_grid() is_mouse_over_grid(&g_grid) 1553 | #define is_mouse_clicked() is_mouse_clicked(&g_grid, &g_event) 1554 | #define is_mouse_moved() is_mouse_moved(&g_grid, &g_event) 1555 | #define get_mouse_pos_x() get_mouse_pos_x(&g_grid, &g_event) 1556 | #define get_mouse_pos_y() get_mouse_pos_y(&g_grid, &g_event) 1557 | #define get_mouse_button() get_mouse_button(&g_grid, &g_event) 1558 | #define exit() exit(0) 1559 | #define draw_key(key, at_x, at_y, color) draw_key(&g_grid, key, at_x, at_y, color) 1560 | #define draw_char(c, at_x, at_y, color) draw_char(&g_grid, c, at_x, at_y, color) 1561 | #define draw_text(text, at_x, at_y, space, color) draw_text(&g_grid, text, at_x, at_y, space, color) 1562 | #define printf(...) (fprintf (stdout, __VA_ARGS__), fflush(stdout)) 1563 | 1564 | //*************************************************************************************** 1565 | // DOCUMENTATION 1566 | //*************************************************************************************** 1567 | 1568 | //*************************************************************************************** 1569 | // 1570 | // Available function: 1571 | // 1572 | // set_background_color(color) 1573 | // Set the background color to one of the predefined colors 1574 | // 1575 | // get_background_color() 1576 | // Get the current background color 1577 | // 1578 | // set_cell_color(x, y, color) 1579 | // Set the color of the cell at coordinates (x, y) 1580 | // 1581 | // get_cell_color(x, y) 1582 | // Get the current color of the cell at coordinates (x, y) 1583 | // 1584 | // set_cell_border_color(x, y, color) 1585 | // Set the border color of the cell at coordinates (x, y) 1586 | // 1587 | // get_cell_border_color(x, y) 1588 | // Get the current border color of the cell at coordinates (x, y) 1589 | // 1590 | // set_grid_color(color) 1591 | // Set the grid (all cells) color 1592 | // 1593 | // get_grid_color() 1594 | // Get the grid color 1595 | // 1596 | // set_grid_border_color(color) 1597 | // Set the grid (and cells) border color 1598 | // 1599 | // get_grid_border_color() 1600 | // Get the current grid border color 1601 | // 1602 | // delay(ms) 1603 | // Wait a specified number of milliseconds 1604 | // 1605 | // get_key() 1606 | // Get the pressed key 1607 | // 1608 | // is_mouse_over_grid() 1609 | // Check if the mouse is over the grid. 1610 | // 1611 | // is_mouse_clicked() 1612 | // Check if the mouse was clicked (over the grid). 1613 | // 1614 | // is_mouse_moved() 1615 | // Check if the mouse was moved (over the grid). 1616 | // 1617 | // get_mouse_pos_x() 1618 | // Get the x coordinate of mouse. 1619 | // 1620 | // get_mouse_pos_y() 1621 | // Get the y coordinate of mouse. 1622 | // 1623 | // get_mouse_button() 1624 | // Get the clicked mouse button (SDL_BUTTON_LEFT, SDL_BUTTON_MIDDLE, SDL_BUTTON_RIGHT). 1625 | // 1626 | // draw_key(key, at_x, at_y, color) 1627 | // Write a key at position (at_x, at_y) with a specific color 1628 | // 1629 | // draw_char(c, at_x, at_y, color) 1630 | // Write a character at position (at_x, at_y) with a specific color 1631 | // 1632 | // draw_text(text, at_x, at_y, space, color) 1633 | // Write a text at position (at_x, at_y) with a specific color 1634 | // The space parameter represents the spacing between characters 1635 | // 1636 | // is_key_ascii(key) 1637 | // Check if a key is an ASCII-based character 1638 | // 1639 | // is_key_letter(key) 1640 | // Check if a key is a letter 1641 | // 1642 | // is_key_digit(key) 1643 | // Check if a key is a digit 1644 | // 1645 | // is_key_arithmetic_op(key) 1646 | // Check if a key is an arithmetic operator (+, -, *, /, %) 1647 | // 1648 | // key_to_char(key) 1649 | // Convert a key to a character 1650 | // 1651 | // key_to_char_lowercase(key) 1652 | // Convert a key to a lowercase character 1653 | // 1654 | // key_to_int(key) 1655 | // Convert a key to a digit (integer) 1656 | // 1657 | // digit_to_key(digit) 1658 | // Convert a digit (integer) to a key 1659 | // 1660 | // digit_to_char(digit) 1661 | // Convert a digit (integer) to a character 1662 | // 1663 | // char_to_digit(c) 1664 | // Convert a character to a digit (integer) 1665 | // 1666 | // random_int(min, max) 1667 | // Get a random integer between min and max 1668 | // 1669 | // is_color_equal(color1, color2) 1670 | // Check if 'color1' is equal to 'color2' (return a boolean) 1671 | // 1672 | // exit() 1673 | // Exit the program 1674 | // 1675 | // Available colors: 1676 | // 1677 | // COLOR_BLACK 1678 | // COLOR_WHITE 1679 | // COLOR_GRAY 1680 | // COLOR_DARK_GRAY 1681 | // COLOR_LIGHT_GRAY 1682 | // COLOR_RED 1683 | // COLOR_GREEN 1684 | // COLOR_BLUE 1685 | // COLOR_YELLOW 1686 | // COLOR_ORANGE 1687 | // COLOR_PINK 1688 | // COLOR_VIOLET 1689 | // 1690 | // Custom color: 1691 | // 1692 | // COLOR( red, green, blue ) 1693 | // 1694 | // Available keys: 1695 | // (Full list at https://wiki.libsdl.org/SDL_Keycode) 1696 | // 1697 | // SDLK_RETURN 1698 | // SDLK_SPACE 1699 | // SDLK_BACKSPACE 1700 | // SDLK_ESCAPE 1701 | // SDLK_{character} 1702 | // SDLK_{digit} 1703 | // SDLK_UP 1704 | // SDLK_DOWN 1705 | // SDLK_RIGHT 1706 | // SDLK_LEFT 1707 | // SDLK_EXCLAIM 1708 | // SDLK_PERIOD 1709 | // SDLK_COLON 1710 | // SDLK_EQUALS 1711 | // SDLK_PLUS 1712 | // SDLK_MINUS 1713 | // SDLK_DIVIDE 1714 | // SDLK_MULTIPLY 1715 | // SDLK_PERCENT 1716 | // 1717 | // 1718 | //*************************************************************************************** 1719 | 1720 | //*************************************************************************************** 1721 | // FREE CODING SECTION 1722 | //*************************************************************************************** 1723 | 1724 | void draw(int x_cells, int y_cells) 1725 | { 1726 | delay(500); 1727 | 1728 | set_cell_color(2, 1, COLOR_RED); 1729 | 1730 | delay(500); 1731 | 1732 | set_cell_color(2, 3, COLOR_BLUE); 1733 | 1734 | delay(500); 1735 | 1736 | set_cell_color(2, 5, COLOR_GREEN); 1737 | 1738 | delay(500); 1739 | 1740 | set_cell_color(2, 7, COLOR_YELLOW); 1741 | } 1742 | --------------------------------------------------------------------------------