Drawing Graphics by using C Programing Language

This is a C Language Program to show the drawing and filling of some basic shapes like rectangle, circle etc. with colors and different shades by using Turbo C graphics.h and modern SDL libraries.

Drawing Graphics by using C Programing Language

Small C Language Program to show the drawing and filling of some basic shapes like rectangle, circle etc. with colors and different shades. First we created a characters array of patterns that will fill the drawing, then initialize the graphics mode and the draw the shapes.

Note: The graphics.h library and functions used in this code are specific to the Turbo C compiler. Modern compilers might not support these graphics functions directly. We have re-written the same C source code by using a more modern graphics library called SDL (Simple DirectMedia Layer), which is cross-platform and widely used for multimedia applications, including graphics.

By using graphics.h Library

#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>

int main() {
    int gd = DETECT, gm, maxx, maxy, x = 40, y = 40, fst;
    char str[40];
    char *pattern[] = {
        "EMPTY_FILL", "SOLID_FILL", "LTSLASH_FILL", "SLASH_FILL", "BKSLASH_FILL",
        "LTBKSLASH_FILL", "HATCH_FILL", "XHATCH_FILL", "INTERLEAVE_FILL",
        "WIDE_DOR_FILL", "CLOSE_DOT_FILL", "USER_FILL"
    };

    initgraph(&gd, &gm, NULL);
    maxx = getmaxx();
    maxy = getmaxy();
    rectangle(0, 10, maxx, maxy);
    setcolor(WHITE);
    outtextxy(175, 0, "Pre-Defined Fill styles");

    for (fst = 0; fst < 12; fst++) {
        setfillstyle(fst, MAGENTA);
        bar(x, y, x + 80, y + 80);
        itoa(fst, str, 10);
        outtextxy(x, y + 100, str);
        outtextxy(x, y + 120, pattern[fst]); // Display the fill pattern name
        x = x + 150;
        if (x > 490) {
            y = y + 150;
            x = 40;
        }
    }

    getch();
    closegraph();
    return 0;
}

By using SDL Graphics Library

The SDL library provides a different approach to graphics than the traditional BGI graphics. In this example we draw rectangles directly onto the window using the SDL rendering functions. Additionally, SDL does not have built-in text rendering so we used a separate library called SDL_ttf for text rendering, as demonstrated in the code.

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

void drawRectangle(SDL_Renderer *renderer, int x, int y, int w, int h, Uint8 r, Uint8 g, Uint8 b) {
    SDL_SetRenderDrawColor(renderer, r, g, b, SDL_ALPHA_OPAQUE);
    SDL_Rect rect = { x, y, w, h };
    SDL_RenderFillRect(renderer, &rect);
}

int main() {
    SDL_Init(SDL_INIT_VIDEO);
    
    SDL_Window* window = SDL_CreateWindow("Pre-Defined Fill Styles", 
    SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

    int x = 40, y = 40;
    char str[40];
    char *pattern[] = {
        "EMPTY_FILL", "SOLID_FILL", "LTSLASH_FILL", "SLASH_FILL", "BKSLASH_FILL",
        "LTBKSLASH_FILL", "HATCH_FILL", "XHATCH_FILL", "INTERLEAVE_FILL",
        "WIDE_DOR_FILL", "CLOSE_DOT_FILL", "USER_FILL"
    };

    SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
    SDL_RenderClear(renderer);

    SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
    for (int fst = 0; fst < 12; fst++) {
        drawRectangle(renderer, x, y, 80, 80, 255, 0, 255);
        sprintf(str, "%d", fst);
        SDL_Surface* surface = TTF_RenderText_Solid(font, str, 
        (SDL_Color){0, 0, 0, 255});
        SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
        SDL_FreeSurface(surface);
        SDL_Rect textRect = { x, y + 100, 20, 20 };
        SDL_RenderCopy(renderer, texture, NULL, &textRect);

        surface = TTF_RenderText_Solid(font, pattern[fst], (SDL_Color){0, 0, 0, 255});
        texture = SDL_CreateTextureFromSurface(renderer, surface);
        SDL_FreeSurface(surface);
        textRect.y += 20;
        SDL_RenderCopy(renderer, texture, NULL, &textRect);

        x = x + 150;
        if (x > 490) {
            y = y + 150;
            x = 40;
        }
    }

    SDL_RenderPresent(renderer);

    SDL_Delay(3000);

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}
Scroll to Top