From ce466e83aecde146d655a34414f5abae543639a1 Mon Sep 17 00:00:00 2001 From: "Amine B. Hassouna" Date: Sun, 28 Oct 2018 15:08:04 +0100 Subject: [PATCH] Add source code and build system config --- .gitignore | 2 + CMakeLists.txt | 64 +++++++++++++++++++++ include/.include_dir | 1 + src/main.c | 132 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 199 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 include/.include_dir create mode 100644 src/main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1b1295f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build +CMakeLists.txt.* diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..e5aada1 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,64 @@ +# +# Copyright (c) 2018 Amine Ben Hassouna +# +# Permission is hereby granted, free of charge, to any +# person obtaining a copy of this software and associated +# documentation files (the "Software"), to deal in the +# Software without restriction, including without +# limitation the rights to use, copy, modify, merge, +# publish, distribute, sublicense, and/or sell copies of +# the Software, and to permit persons to whom the Software +# is furnished to do so, subject to the following +# conditions: +# +# The above copyright notice and this permission notice +# shall be included in all copies or substantial portions +# of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +# ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +# TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +# SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +# IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. +# + +# Define the project name +project(basic-c-sdl-project) + +# Set the cmake minimum version to 3.5.1 +cmake_minimum_required(VERSION 3.5.1) + +# Add all headers files under the include directory +include_directories(include) + +# Add all c source files under the src directory +file(GLOB SOURCES "src/*.c") +add_executable(${PROJECT_NAME} ${SOURCES}) + +# Load CMake module : FindPkgConfig +include(FindPkgConfig) + +# Add SDL2 library +pkg_search_module(SDL2 REQUIRED sdl2) +include_directories(${SDL2_INCLUDE_DIRS}) +target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES}) + +# Add SDL2_image library +#pkg_search_module(SDL2IMAGE REQUIRED SDL2_image>=2.0.0) +#include_directories(${SDL2IMAGE_INCLUDE_DIRS}) +#target_link_libraries(${PROJECT_NAME} ${SDL2IMAGE_LIBRARIES}) + +# Add SDL2_ttf library +#pkg_search_module(SDL2TTF REQUIRED SDL2_ttf>=2.0.0) +#include_directories(${SDL2TTF_INCLUDE_DIRS}) +#target_link_libraries(${PROJECT_NAME} ${SDL2TTF_LIBRARIES}) + +# Add SDL2_gfx library +#pkg_search_module(SDL2GFX REQUIRED SDL2_gfx) +#include_directories(${SDL2GFX_INCLUDE_DIRS}) +#target_link_libraries(${PROJECT_NAME} ${SDL2GFX_LIBRARIES}) + diff --git a/include/.include_dir b/include/.include_dir new file mode 100644 index 0000000..f47d6fb --- /dev/null +++ b/include/.include_dir @@ -0,0 +1 @@ +This is the include directory diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..a692e6d --- /dev/null +++ b/src/main.c @@ -0,0 +1,132 @@ +/* + * Copyright (c) 2018 Amine Ben Hassouna + * + * Permission is hereby granted, free of charge, to any + * person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the + * Software without restriction, including without + * limitation the rights to use, copy, modify, merge, + * publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software + * is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice + * shall be included in all copies or substantial portions + * of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF + * ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED + * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT + * SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR + * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + */ + +#include +#include + +#include + +// Define MAX and MIN macros +#define MAX(X, Y) (((X) > (Y)) ? (X) : (Y)) +#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) + +// Define screen dimensions +#define SCREEN_WIDTH 800 +#define SCREEN_HEIGHT 600 + +int main(int argc, char* args[]) +{ + // Initialize SDL + if(SDL_Init(SDL_INIT_VIDEO) < 0) + { + printf("SDL could not be initialized!\n" + "SDL_Error: %s\n", SDL_GetError()); + return 0; + } + + // Create window + SDL_Window *window = SDL_CreateWindow("Basic C SDL project", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + SCREEN_WIDTH, SCREEN_HEIGHT, + SDL_WINDOW_SHOWN); + if(!window) + { + printf("Window could not be created!\n" + "SDL_Error: %s\n", SDL_GetError()); + } + else + { + // Create renderer + SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); + if(!renderer) + { + printf("Renderer could not be created!\n" + "SDL_Error: %s\n", SDL_GetError()); + } + else + { + // Declare rect of square + SDL_Rect squareRect; + + // Square dimensions: Half of the min(SCREEN_WIDTH, SCREEN_HEIGHT) + squareRect.w = MIN(SCREEN_WIDTH, SCREEN_HEIGHT) / 2; + squareRect.h = MIN(SCREEN_WIDTH, SCREEN_HEIGHT) / 2; + + // Square position: In the middle of the screen + squareRect.x = SCREEN_WIDTH / 2 - squareRect.w / 2; + squareRect.y = SCREEN_HEIGHT / 2 - squareRect.h / 2; + + + // Event loop exit flag + bool quit = false; + + // Event loop + while(!quit) + { + SDL_Event e; + + // Wait indefinitely for the next available event + SDL_WaitEvent(&e); + + // User requests quit + if(e.type == SDL_QUIT) + { + quit = true; + } + + // Initialize renderer color white for the background + SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); + + // Clear screen + SDL_RenderClear(renderer); + + // Set renderer color red to draw the square + SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF); + + // Draw filled square + SDL_RenderFillRect(renderer, &squareRect); + + // Update screen + SDL_RenderPresent(renderer); + } + + // Destroy renderer + SDL_DestroyRenderer(renderer); + } + } + + // Destroy window + SDL_DestroyWindow(window); + + // Quit SDL + SDL_Quit(); + + return 0; +}