Game Programming Using C

Game Programming Using C

PART 1

·

1 min read

In the following series of blogs, I will show game programming is done using C programming.

SDL is API used for game development in C.SDL is used in C but it is object-oriented, yes it is strange that C is not object-oriented but SDL which API for C programs is object-oriented.

Code is written with comments for explaination

#include <stdio.h>
#include <SDL2/SDL.h>

typedef struct Man
{
    int x,y;
    short life;
    char* name;
} Man;

int main(int argc, char const *argv[])
{
    // lets setup the SDL code


    // Declare the window
    SDL_Window* window;

    // declare the renderer
    SDL_Renderer* renderer;

    // Create the application window with specific settings
    //1.Window title
    //2.initial x position
    //3.initial y position
    //4.width 
    //5.height
    //6.flags
    window = SDL_CreateWindow("Game Window",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,640,480,0);
    renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED);

    SDL_SetRenderDrawColor(renderer,0,0,255,255);

    // clear the screen to blue
    SDL_RenderClear(renderer);

    // set the drawing color to white
    SDL_SetRenderDrawColor(renderer,255,255,255,255);

    SDL_Rect rect = {220,140,200,200};
    SDL_RenderFillRect(renderer,&rect);

    // present what we have drawn
    SDL_RenderPresent(renderer);

    // wait few seconds before quitting
    SDL_Delay(2000);

    // close and destroy the window
    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(renderer);

    // Clean up
    SDL_Quit();



    // get out of function
    return 0;
}

Use the following in the terminal for compiling to avoid the error of SDL

gcc -g main.c -o ../ludo -lSDL2

That's it part 1 is done. You will see a blue white color window for 2 seconds on screen.