Graphics Library (graphics.h) Reference (part 3)

graphics.h Reference

In the previous two posts (Graphics Library Reference part 1 and part 2) I have discussed few important functions of graphics.h library in C Programming. As promised here is the sample C language program which will demonstrate the use of graphics functions using C language and draw some geometrical shapes on the console. If you have followed the previous two posts the you will not find the source code difficult to compile and execute. If you still experience any problems using the source code or compiling the code the please do let me know, and I will try to figure it out.

In the meantime you can browse through more source code for C Programming and C++ Programming and even more source code under Source Code section.

#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <process.h>

   void main(void) {
      int mx, my;
      /* request auto detection */      int gd = DETECT, gm, errorcode;
      initgraph( &gd, &gm, "F:\tc\bgi"); //Last argument is the path to the graphics library of Turbo C
      /* read result of initialization */      errorcode = graphresult();
      if (errorcode != grOk) /* an error occurred */ {
         printf("Graphics error: %sn", grapherrormsg(errorcode));
         printf("Press any key to halt:");
         exit(1); /* return with error code */      }
      mx = (getmaxx() / 2);
      my = (getmaxy() / 2);
      //SET baqckground color
      setfillstyle(9, 1);
      bar(0, 0, getmaxx(), getmaxy());
      //DRAW a bar, and make it look like a 3d bar
      setfillstyle(1, 7);
      //DRAW lines for the top and left side
      line(50, 20, 600, 20);
      line(51, 21, 599, 21);
      line(50, 20, 50, 400);
      line(51, 21, 51, 399);
      //DRAW lines for the bottom and right side
      line(600, 20, 600, 400);
      line(599, 21, 599, 400);
      line(50, 400, 600, 400);
      line(51, 399, 600, 399);
      //DRAW two 3D bars for the left and right side
      setfillstyle(9, 8);
      bar(70, 40, 100, 380);
      bar(70, 40, 550, 70);
      bar(70, 350, 550, 379);
      bar(545, 40, 575, 380);
      //PRINT 3D text CALENDAR 2002
      settextstyle(1, 0, 4);
      settextjustify(1, 1);
      outtextxy(mx + 2, my - 46, "Thank You!");
      outtextxy(mx + 1, my - 45, "Thank You!");
      outtextxy(mx + 2, my - 44, "Thank You!");
      //PRINT 3D text 2002
      outtextxy(mx, my + 10, "2006");
      outtextxy(mx + 1, my + 11, "2006");
      outtextxy(mx + 2, my + 12, "2006");
      //PRINT copyright notice
      settextstyle(2, 0, 5);
      outtextxy(mx + 1, my + 150, "Copyright");
      //Print the circle around the text
      //the text is "Thank You!"
      circle(mx, my - 30, 120);
      //Print the ellipse around the text
      //the text is "Thank You!"
      ellipse(mx, my - 30, 0, 360, 180, 70);
      //Print the 3D line below the text
      //the text is "Thank You!"
      line(230, 220, 410, 220);
      bar3d(235, 225, 415, 225, 4, 1);
      getch(); //PAUSE for a while

I hope this explanation of common functions present in graphics.h library and the sample code would help you learn graphics programming in C language.

Here’s Must-Read List of C programming books for beginners

C Programming Language, 2nd Edition

With over 600 5-star reviews on Amazon, readers agree that C Programming Language, 2nd Edition by Brian Kernighan and Dennis Ritchie is the best C Programming book for Beginners. The authors present the complete guide to ANSI standard C language programming. Written by the developers of C, this new version helps readers keep up with the finalized ANSI standard for C while showing how to take advantage of C’s rich set of operators, economy of expression, improved control flow, and data structures.

21st Century C: C Tips from the New School

Throw out your old ideas about C and get to know a programming language that’s substantially outgrown its origins. With this revised edition of 21st Century C, you’ll discover up-to-date techniques missing from other C tutorials, whether you’re new to the language or just getting reacquainted.

C Programming Absolute Beginner’s Guide

Greg Perry is the author of over 75 computer books and known for bringing programming topics down to the beginner’s level. His book C Programming Absolute Beginner’s Guide, is today’s best beginner’s guide to writing C programs–and to learning skills to use with practically any language. Its simple, practical instructions will help you start creating useful, reliable C code, from games to mobile apps. Plus, it’s fully updated for the new C11 standard and today’s free, open source tools!

C Programming: A Modern Approach, 2nd Edition

KN King tackles on some C standard library specifics header by header in his C Programming: A Modern Approach book. The second edition maintains all the book’s popular features and brings it up to date with coverage of the C99 standard. The new edition also adds a significant number of exercises and longer programming projects, and includes extensive revisions and updates.

Programming Arduino: Getting Started with Sketches

Simon Monk, a Ph.D. in software engineering, writes this great little book for learning to program the arduino using C language. This bestselling guide explains how to write well-crafted sketches using Arduino’s modified C language. You will learn how to configure hardware and software, develop your own sketches, work with built-in and custom Arduino libraries, and explore the Internet of Things—all with no prior programming experience required!

Categories: Blog
M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on since 2004.
Related Post