Add source code and build system config
This commit is contained in:
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