├── .gitattributes ├── CMakeLists.txt ├── LICENSE ├── README.md ├── cmake └── FindSDL2.cmake ├── main.c └── screenshot.png /.gitattributes: -------------------------------------------------------------------------------- 1 | cmake/* linguist-vendored 2 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.9) 2 | project(sdl-grid C) 3 | 4 | set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake") 5 | 6 | find_package(SDL2 REQUIRED) 7 | include_directories(${SDL2_INCLUDE_DIR}) 8 | 9 | add_executable(${PROJECT_NAME} main.c) 10 | 11 | target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARY}) 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Creative Commons Legal Code 2 | 3 | CC0 1.0 Universal 4 | 5 | CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE 6 | LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN 7 | ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS 8 | INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES 9 | REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS 10 | PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM 11 | THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED 12 | HEREUNDER. 13 | 14 | Statement of Purpose 15 | 16 | The laws of most jurisdictions throughout the world automatically confer 17 | exclusive Copyright and Related Rights (defined below) upon the creator 18 | and subsequent owner(s) (each and all, an "owner") of an original work of 19 | authorship and/or a database (each, a "Work"). 20 | 21 | Certain owners wish to permanently relinquish those rights to a Work for 22 | the purpose of contributing to a commons of creative, cultural and 23 | scientific works ("Commons") that the public can reliably and without fear 24 | of later claims of infringement build upon, modify, incorporate in other 25 | works, reuse and redistribute as freely as possible in any form whatsoever 26 | and for any purposes, including without limitation commercial purposes. 27 | These owners may contribute to the Commons to promote the ideal of a free 28 | culture and the further production of creative, cultural and scientific 29 | works, or to gain reputation or greater distribution for their Work in 30 | part through the use and efforts of others. 31 | 32 | For these and/or other purposes and motivations, and without any 33 | expectation of additional consideration or compensation, the person 34 | associating CC0 with a Work (the "Affirmer"), to the extent that he or she 35 | is an owner of Copyright and Related Rights in the Work, voluntarily 36 | elects to apply CC0 to the Work and publicly distribute the Work under its 37 | terms, with knowledge of his or her Copyright and Related Rights in the 38 | Work and the meaning and intended legal effect of CC0 on those rights. 39 | 40 | 1. Copyright and Related Rights. A Work made available under CC0 may be 41 | protected by copyright and related or neighboring rights ("Copyright and 42 | Related Rights"). Copyright and Related Rights include, but are not 43 | limited to, the following: 44 | 45 | i. the right to reproduce, adapt, distribute, perform, display, 46 | communicate, and translate a Work; 47 | ii. moral rights retained by the original author(s) and/or performer(s); 48 | iii. publicity and privacy rights pertaining to a person's image or 49 | likeness depicted in a Work; 50 | iv. rights protecting against unfair competition in regards to a Work, 51 | subject to the limitations in paragraph 4(a), below; 52 | v. rights protecting the extraction, dissemination, use and reuse of data 53 | in a Work; 54 | vi. database rights (such as those arising under Directive 96/9/EC of the 55 | European Parliament and of the Council of 11 March 1996 on the legal 56 | protection of databases, and under any national implementation 57 | thereof, including any amended or successor version of such 58 | directive); and 59 | vii. other similar, equivalent or corresponding rights throughout the 60 | world based on applicable law or treaty, and any national 61 | implementations thereof. 62 | 63 | 2. Waiver. To the greatest extent permitted by, but not in contravention 64 | of, applicable law, Affirmer hereby overtly, fully, permanently, 65 | irrevocably and unconditionally waives, abandons, and surrenders all of 66 | Affirmer's Copyright and Related Rights and associated claims and causes 67 | of action, whether now known or unknown (including existing as well as 68 | future claims and causes of action), in the Work (i) in all territories 69 | worldwide, (ii) for the maximum duration provided by applicable law or 70 | treaty (including future time extensions), (iii) in any current or future 71 | medium and for any number of copies, and (iv) for any purpose whatsoever, 72 | including without limitation commercial, advertising or promotional 73 | purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each 74 | member of the public at large and to the detriment of Affirmer's heirs and 75 | successors, fully intending that such Waiver shall not be subject to 76 | revocation, rescission, cancellation, termination, or any other legal or 77 | equitable action to disrupt the quiet enjoyment of the Work by the public 78 | as contemplated by Affirmer's express Statement of Purpose. 79 | 80 | 3. Public License Fallback. Should any part of the Waiver for any reason 81 | be judged legally invalid or ineffective under applicable law, then the 82 | Waiver shall be preserved to the maximum extent permitted taking into 83 | account Affirmer's express Statement of Purpose. In addition, to the 84 | extent the Waiver is so judged Affirmer hereby grants to each affected 85 | person a royalty-free, non transferable, non sublicensable, non exclusive, 86 | irrevocable and unconditional license to exercise Affirmer's Copyright and 87 | Related Rights in the Work (i) in all territories worldwide, (ii) for the 88 | maximum duration provided by applicable law or treaty (including future 89 | time extensions), (iii) in any current or future medium and for any number 90 | of copies, and (iv) for any purpose whatsoever, including without 91 | limitation commercial, advertising or promotional purposes (the 92 | "License"). The License shall be deemed effective as of the date CC0 was 93 | applied by Affirmer to the Work. Should any part of the License for any 94 | reason be judged legally invalid or ineffective under applicable law, such 95 | partial invalidity or ineffectiveness shall not invalidate the remainder 96 | of the License, and in such case Affirmer hereby affirms that he or she 97 | will not (i) exercise any of his or her remaining Copyright and Related 98 | Rights in the Work or (ii) assert any associated claims and causes of 99 | action with respect to the Work, in either case contrary to Affirmer's 100 | express Statement of Purpose. 101 | 102 | 4. Limitations and Disclaimers. 103 | 104 | a. No trademark or patent rights held by Affirmer are waived, abandoned, 105 | surrendered, licensed or otherwise affected by this document. 106 | b. Affirmer offers the Work as-is and makes no representations or 107 | warranties of any kind concerning the Work, express, implied, 108 | statutory or otherwise, including without limitation warranties of 109 | title, merchantability, fitness for a particular purpose, non 110 | infringement, or the absence of latent or other defects, accuracy, or 111 | the present or absence of errors, whether or not discoverable, all to 112 | the greatest extent permissible under applicable law. 113 | c. Affirmer disclaims responsibility for clearing rights of other persons 114 | that may apply to the Work or any use thereof, including without 115 | limitation any person's Copyright and Related Rights in the Work. 116 | Further, Affirmer disclaims responsibility for obtaining any necessary 117 | consents, permissions or other rights required for any use of the 118 | Work. 119 | d. Affirmer understands and acknowledges that Creative Commons is not a 120 | party to this document and has no duty or obligation with respect to 121 | this CC0 or use of the Work. 122 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SDL Grid 2 | 3 | A simple grid drawn using [SDL](https://www.libsdl.org), with square transparent cells divided by dark grey lines over a dark background with a white cell cursor that is movable with either the mouse or keyboard input. 4 | 5 | ## Screenshot 6 | 7 | ![Screenshot](screenshot.png?raw=true) 8 | 9 | ## License 10 | 11 | Everything with the exception of the contents of the cmake folder is dedicated 12 | to the public domain under the CC0 1.0 Universal license. 13 | -------------------------------------------------------------------------------- /cmake/FindSDL2.cmake: -------------------------------------------------------------------------------- 1 | # Locate SDL2 library 2 | # This module defines 3 | # SDL2_LIBRARY, the name of the library to link against 4 | # SDL2_FOUND, if false, do not try to link to SDL2 5 | # SDL2_INCLUDE_DIR, where to find SDL.h 6 | # 7 | # This module responds to the the flag: 8 | # SDL2_BUILDING_LIBRARY 9 | # If this is defined, then no SDL2_main will be linked in because 10 | # only applications need main(). 11 | # Otherwise, it is assumed you are building an application and this 12 | # module will attempt to locate and set the the proper link flags 13 | # as part of the returned SDL2_LIBRARY variable. 14 | # 15 | # Don't forget to include SDL2main.h and SDL2main.m your project for the 16 | # OS X framework based version. (Other versions link to -lSDL2main which 17 | # this module will try to find on your behalf.) Also for OS X, this 18 | # module will automatically add the -framework Cocoa on your behalf. 19 | # 20 | # 21 | # Additional Note: If you see an empty SDL2_LIBRARY_TEMP in your configuration 22 | # and no SDL2_LIBRARY, it means CMake did not find your SDL2 library 23 | # (SDL2.dll, libsdl2.so, SDL2.framework, etc). 24 | # Set SDL2_LIBRARY_TEMP to point to your SDL2 library, and configure again. 25 | # Similarly, if you see an empty SDL2MAIN_LIBRARY, you should set this value 26 | # as appropriate. These values are used to generate the final SDL2_LIBRARY 27 | # variable, but when these values are unset, SDL2_LIBRARY does not get created. 28 | # 29 | # 30 | # $SDL2 is an environment variable that would 31 | # correspond to the ./configure --prefix=$SDL2 32 | # used in building SDL2. 33 | # l.e.galup 9-20-02 34 | # 35 | # Modified by Eric Wing. 36 | # Added code to assist with automated building by using environmental variables 37 | # and providing a more controlled/consistent search behavior. 38 | # Added new modifications to recognize OS X frameworks and 39 | # additional Unix paths (FreeBSD, etc). 40 | # Also corrected the header search path to follow "proper" SDL2 guidelines. 41 | # Added a search for SDL2main which is needed by some platforms. 42 | # Added a search for threads which is needed by some platforms. 43 | # Added needed compile switches for MinGW. 44 | # 45 | # On OSX, this will prefer the Framework version (if found) over others. 46 | # People will have to manually change the cache values of 47 | # SDL2_LIBRARY to override this selection or set the CMake environment 48 | # CMAKE_INCLUDE_PATH to modify the search paths. 49 | # 50 | # Note that the header path has changed from SDL2/SDL.h to just SDL.h 51 | # This needed to change because "proper" SDL2 convention 52 | # is #include "SDL.h", not . This is done for portability 53 | # reasons because not all systems place things in SDL2/ (see FreeBSD). 54 | # 55 | # Ported by Johnny Patterson. This is a literal port for SDL2 of the FindSDL.cmake 56 | # module with the minor edit of changing "SDL" to "SDL2" where necessary. This 57 | # was not created for redistribution, and exists temporarily pending official 58 | # SDL2 CMake modules. 59 | # 60 | # Note that on windows this will only search for the 32bit libraries, to search 61 | # for 64bit change x86/i686-w64 to x64/x86_64-w64 62 | 63 | #============================================================================= 64 | # Copyright 2003-2009 Kitware, Inc. 65 | # 66 | # CMake - Cross Platform Makefile Generator 67 | # Copyright 2000-2014 Kitware, Inc. 68 | # Copyright 2000-2011 Insight Software Consortium 69 | # All rights reserved. 70 | # 71 | # Redistribution and use in source and binary forms, with or without 72 | # modification, are permitted provided that the following conditions 73 | # are met: 74 | # 75 | # * Redistributions of source code must retain the above copyright 76 | # notice, this list of conditions and the following disclaimer. 77 | # 78 | # * Redistributions in binary form must reproduce the above copyright 79 | # notice, this list of conditions and the following disclaimer in the 80 | # documentation and/or other materials provided with the distribution. 81 | # 82 | # * Neither the names of Kitware, Inc., the Insight Software Consortium, 83 | # nor the names of their contributors may be used to endorse or promote 84 | # products derived from this software without specific prior written 85 | # permission. 86 | # 87 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 88 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 89 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 90 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 91 | # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 92 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 93 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 94 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 95 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 96 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 97 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 98 | # 99 | # This software is distributed WITHOUT ANY WARRANTY; without even the 100 | # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 101 | # See the License for more information. 102 | #============================================================================= 103 | # (To distribute this file outside of CMake, substitute the full 104 | # License text for the above reference.) 105 | 106 | FIND_PATH(SDL2_INCLUDE_DIR SDL.h 107 | HINTS 108 | ${SDL2} 109 | $ENV{SDL2} 110 | PATH_SUFFIXES include/SDL2 include SDL2 111 | i686-w64-mingw32/include/SDL2 112 | x86_64-w64-mingw32/include/SDL2 113 | PATHS 114 | ~/Library/Frameworks 115 | /Library/Frameworks 116 | /usr/local/include/SDL2 117 | /usr/include/SDL2 118 | /sw # Fink 119 | /opt/local # DarwinPorts 120 | /opt/csw # Blastwave 121 | /opt 122 | ) 123 | 124 | # Lookup the 64 bit libs on x64 125 | IF(CMAKE_SIZEOF_VOID_P EQUAL 8) 126 | FIND_LIBRARY(SDL2_LIBRARY_TEMP SDL2 127 | HINTS 128 | ${SDL2} 129 | $ENV{SDL2} 130 | PATH_SUFFIXES lib64 lib 131 | lib/x64 132 | x86_64-w64-mingw32/lib 133 | PATHS 134 | /sw 135 | /opt/local 136 | /opt/csw 137 | /opt 138 | ) 139 | # On 32bit build find the 32bit libs 140 | ELSE(CMAKE_SIZEOF_VOID_P EQUAL 8) 141 | FIND_LIBRARY(SDL2_LIBRARY_TEMP SDL2 142 | HINTS 143 | ${SDL2} 144 | $ENV{SDL2} 145 | PATH_SUFFIXES lib 146 | lib/x86 147 | i686-w64-mingw32/lib 148 | PATHS 149 | /sw 150 | /opt/local 151 | /opt/csw 152 | /opt 153 | ) 154 | ENDIF(CMAKE_SIZEOF_VOID_P EQUAL 8) 155 | 156 | IF(NOT SDL2_BUILDING_LIBRARY) 157 | IF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework") 158 | # Non-OS X framework versions expect you to also dynamically link to 159 | # SDL2main. This is mainly for Windows and OS X. Other (Unix) platforms 160 | # seem to provide SDL2main for compatibility even though they don't 161 | # necessarily need it. 162 | # Lookup the 64 bit libs on x64 163 | IF(CMAKE_SIZEOF_VOID_P EQUAL 8) 164 | FIND_LIBRARY(SDL2MAIN_LIBRARY 165 | NAMES SDL2main 166 | HINTS 167 | ${SDL2} 168 | $ENV{SDL2} 169 | PATH_SUFFIXES lib64 lib 170 | lib/x64 171 | x86_64-w64-mingw32/lib 172 | PATHS 173 | /sw 174 | /opt/local 175 | /opt/csw 176 | /opt 177 | ) 178 | # On 32bit build find the 32bit libs 179 | ELSE(CMAKE_SIZEOF_VOID_P EQUAL 8) 180 | FIND_LIBRARY(SDL2MAIN_LIBRARY 181 | NAMES SDL2main 182 | HINTS 183 | ${SDL2} 184 | $ENV{SDL2} 185 | PATH_SUFFIXES lib 186 | lib/x86 187 | i686-w64-mingw32/lib 188 | PATHS 189 | /sw 190 | /opt/local 191 | /opt/csw 192 | /opt 193 | ) 194 | ENDIF(CMAKE_SIZEOF_VOID_P EQUAL 8) 195 | ENDIF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework") 196 | ENDIF(NOT SDL2_BUILDING_LIBRARY) 197 | 198 | # SDL2 may require threads on your system. 199 | # The Apple build may not need an explicit flag because one of the 200 | # frameworks may already provide it. 201 | # But for non-OSX systems, I will use the CMake Threads package. 202 | IF(NOT APPLE) 203 | FIND_PACKAGE(Threads) 204 | ENDIF(NOT APPLE) 205 | 206 | # MinGW needs an additional library, mwindows 207 | # It's total link flags should look like -lmingw32 -lSDL2main -lSDL2 -lmwindows 208 | # (Actually on second look, I think it only needs one of the m* libraries.) 209 | IF(MINGW) 210 | SET(MINGW32_LIBRARY mingw32 CACHE STRING "mwindows for MinGW") 211 | ENDIF(MINGW) 212 | 213 | SET(SDL2_FOUND "NO") 214 | IF(SDL2_LIBRARY_TEMP) 215 | # For SDL2main 216 | IF(NOT SDL2_BUILDING_LIBRARY) 217 | IF(SDL2MAIN_LIBRARY) 218 | SET(SDL2_LIBRARY_TEMP ${SDL2MAIN_LIBRARY} ${SDL2_LIBRARY_TEMP}) 219 | ENDIF(SDL2MAIN_LIBRARY) 220 | ENDIF(NOT SDL2_BUILDING_LIBRARY) 221 | 222 | # For OS X, SDL2 uses Cocoa as a backend so it must link to Cocoa. 223 | # CMake doesn't display the -framework Cocoa string in the UI even 224 | # though it actually is there if I modify a pre-used variable. 225 | # I think it has something to do with the CACHE STRING. 226 | # So I use a temporary variable until the end so I can set the 227 | # "real" variable in one-shot. 228 | IF(APPLE) 229 | SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} "-framework Cocoa") 230 | ENDIF(APPLE) 231 | 232 | # For threads, as mentioned Apple doesn't need this. 233 | # In fact, there seems to be a problem if I used the Threads package 234 | # and try using this line, so I'm just skipping it entirely for OS X. 235 | IF(NOT APPLE) 236 | SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT}) 237 | ENDIF(NOT APPLE) 238 | 239 | # For MinGW library 240 | IF(MINGW) 241 | SET(SDL2_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL2_LIBRARY_TEMP}) 242 | ENDIF(MINGW) 243 | 244 | # Set the final string here so the GUI reflects the final state. 245 | SET(SDL2_LIBRARY ${SDL2_LIBRARY_TEMP} CACHE STRING "Where the SDL2 Library can be found") 246 | # Set the temp variable to INTERNAL so it is not seen in the CMake GUI 247 | SET(SDL2_LIBRARY_TEMP "${SDL2_LIBRARY_TEMP}" CACHE INTERNAL "") 248 | 249 | SET(SDL2_FOUND "YES") 250 | ENDIF(SDL2_LIBRARY_TEMP) 251 | 252 | INCLUDE(FindPackageHandleStandardArgs) 253 | 254 | FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2 REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR) 255 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int grid_cell_size = 36; 6 | int grid_width = 29; 7 | int grid_height = 23; 8 | 9 | // + 1 so that the last grid lines fit in the screen. 10 | int window_width = (grid_width * grid_cell_size) + 1; 11 | int window_height = (grid_height * grid_cell_size) + 1; 12 | 13 | // Place the grid cursor in the middle of the screen. 14 | SDL_Rect grid_cursor = { 15 | .x = (grid_width - 1) / 2 * grid_cell_size, 16 | .y = (grid_height - 1) / 2 * grid_cell_size, 17 | .w = grid_cell_size, 18 | .h = grid_cell_size, 19 | }; 20 | 21 | // The cursor ghost is a cursor that always shows in the cell below the 22 | // mouse cursor. 23 | SDL_Rect grid_cursor_ghost = {grid_cursor.x, grid_cursor.y, grid_cell_size, 24 | grid_cell_size}; 25 | 26 | // Dark theme. 27 | SDL_Color grid_background = {22, 22, 22, 255}; // Barely Black 28 | SDL_Color grid_line_color = {44, 44, 44, 255}; // Dark grey 29 | SDL_Color grid_cursor_ghost_color = {44, 44, 44, 255}; 30 | SDL_Color grid_cursor_color = {255, 255, 255, 255}; // White 31 | 32 | // Light Theme. 33 | // SDL_Color grid_background = {233, 233, 233, 255}; // Barely white 34 | // SDL_Color grid_line_color = {200, 200, 200, 255}; // Very light grey 35 | // SDL_Color grid_cursor_ghost_color = {200, 200, 200, 255}; 36 | // SDL_Color grid_cursor_color = {160, 160, 160, 255}; // Grey 37 | 38 | if (SDL_Init(SDL_INIT_VIDEO) < 0) { 39 | SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Initialize SDL: %s", 40 | SDL_GetError()); 41 | return EXIT_FAILURE; 42 | } 43 | 44 | SDL_Window *window; 45 | SDL_Renderer *renderer; 46 | if (SDL_CreateWindowAndRenderer(window_width, window_height, 0, &window, 47 | &renderer) < 0) { 48 | SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, 49 | "Create window and renderer: %s", SDL_GetError()); 50 | return EXIT_FAILURE; 51 | } 52 | 53 | SDL_SetWindowTitle(window, "SDL Grid"); 54 | 55 | SDL_bool quit = SDL_FALSE; 56 | SDL_bool mouse_active = SDL_FALSE; 57 | SDL_bool mouse_hover = SDL_FALSE; 58 | 59 | while (!quit) { 60 | SDL_Event event; 61 | while (SDL_PollEvent(&event)) { 62 | switch (event.type) { 63 | case SDL_KEYDOWN: 64 | switch (event.key.keysym.sym) { 65 | case SDLK_w: 66 | case SDLK_UP: 67 | grid_cursor.y -= grid_cell_size; 68 | break; 69 | case SDLK_s: 70 | case SDLK_DOWN: 71 | grid_cursor.y += grid_cell_size; 72 | break; 73 | case SDLK_a: 74 | case SDLK_LEFT: 75 | grid_cursor.x -= grid_cell_size; 76 | break; 77 | case SDLK_d: 78 | case SDLK_RIGHT: 79 | grid_cursor.x += grid_cell_size; 80 | break; 81 | } 82 | break; 83 | case SDL_MOUSEBUTTONDOWN: 84 | grid_cursor.x = (event.motion.x / grid_cell_size) * grid_cell_size; 85 | grid_cursor.y = (event.motion.y / grid_cell_size) * grid_cell_size; 86 | break; 87 | case SDL_MOUSEMOTION: 88 | grid_cursor_ghost.x = (event.motion.x / grid_cell_size) * grid_cell_size; 89 | grid_cursor_ghost.y = (event.motion.y / grid_cell_size) * grid_cell_size; 90 | 91 | if (!mouse_active) 92 | mouse_active = SDL_TRUE; 93 | break; 94 | case SDL_WINDOWEVENT: 95 | if (event.window.event == SDL_WINDOWEVENT_ENTER && !mouse_hover) 96 | mouse_hover = SDL_TRUE; 97 | else if (event.window.event == SDL_WINDOWEVENT_LEAVE && mouse_hover) 98 | mouse_hover = SDL_FALSE; 99 | break; 100 | case SDL_QUIT: 101 | quit = SDL_TRUE; 102 | break; 103 | } 104 | } 105 | 106 | // Draw grid background. 107 | SDL_SetRenderDrawColor(renderer, grid_background.r, grid_background.g, 108 | grid_background.b, grid_background.a); 109 | SDL_RenderClear(renderer); 110 | 111 | // Draw grid lines. 112 | SDL_SetRenderDrawColor(renderer, grid_line_color.r, grid_line_color.g, 113 | grid_line_color.b, grid_line_color.a); 114 | 115 | for (int x = 0; x < 1 + grid_width * grid_cell_size; 116 | x += grid_cell_size) { 117 | SDL_RenderDrawLine(renderer, x, 0, x, window_height); 118 | } 119 | 120 | for (int y = 0; y < 1 + grid_height * grid_cell_size; 121 | y += grid_cell_size) { 122 | SDL_RenderDrawLine(renderer, 0, y, window_width, y); 123 | } 124 | 125 | // Draw grid ghost cursor. 126 | if (mouse_active && mouse_hover) { 127 | SDL_SetRenderDrawColor(renderer, grid_cursor_ghost_color.r, 128 | grid_cursor_ghost_color.g, 129 | grid_cursor_ghost_color.b, 130 | grid_cursor_ghost_color.a); 131 | SDL_RenderFillRect(renderer, &grid_cursor_ghost); 132 | } 133 | 134 | // Draw grid cursor. 135 | SDL_SetRenderDrawColor(renderer, grid_cursor_color.r, 136 | grid_cursor_color.g, grid_cursor_color.b, 137 | grid_cursor_color.a); 138 | SDL_RenderFillRect(renderer, &grid_cursor); 139 | 140 | SDL_RenderPresent(renderer); 141 | } 142 | 143 | SDL_DestroyRenderer(renderer); 144 | SDL_DestroyWindow(window); 145 | SDL_Quit(); 146 | 147 | return EXIT_SUCCESS; 148 | } 149 | -------------------------------------------------------------------------------- /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/catsocks/sdl-grid/9aecdc81c2b115a1624041fddba68b5fc03660ab/screenshot.png --------------------------------------------------------------------------------