Add source code and build system config
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
build
|
||||
CMakeLists.txt.*
|
||||
64
CMakeLists.txt
Normal file
64
CMakeLists.txt
Normal file
@@ -0,0 +1,64 @@
|
||||
#
|
||||
# Copyright (c) 2018 Amine Ben Hassouna <amine.benhassouna@gmail.com>
|
||||
#
|
||||
# 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})
|
||||
|
||||
1
include/.include_dir
Normal file
1
include/.include_dir
Normal file
@@ -0,0 +1 @@
|
||||
This is the include directory
|
||||
132
src/main.c
Normal file
132
src/main.c
Normal file
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* Copyright (c) 2018 Amine Ben Hassouna <amine.benhassouna@gmail.com>
|
||||
*
|
||||
* 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 <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
// 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;
|
||||
}
|
||||
Reference in New Issue
Block a user