Solved

How to do graphics in C

Posted on 2001-07-31
8
285 Views
Last Modified: 2010-04-15
I'm learning C using MS Visual c++
Can I do graphics in C?
Something easy like coloured text or maybe simple graphics?
ASCII is ok but want to do more.
Thanks.
0
Comment
Question by:kapai
  • 5
  • 3
8 Comments
 
LVL 33

Expert Comment

by:hongjun
ID: 6339923
MS Visual C++? Ok try this

#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>

void SetAttribute(HANDLE hOutput, int x, int y, int z, unsigned short Attribute);
void PrintXY(HANDLE hOutput, int x, int y, char *string);


void main()
{
     HANDLE hOutput;

     hOutput = GetStdHandle(STD_OUTPUT_HANDLE);

     SetAttribute(hOutput, 1, 1, strlen("Hello World"), FOREGROUND_RED);
     PrintXY(hOutput, 0, 0, "Hello World");
}



// set the color of text
void SetAttribute(HANDLE hOutput, int x, int y, int z, unsigned short Attribute)
{
     DWORD cWritten;
     COORD coord;

     for ( int i=0; i<z; i++)
     {
          coord.X = x + i;
          coord.Y = y;
          WriteConsoleOutputAttribute(
               hOutput,    // screen buffer handle
               &Attribute, // pointer to source string
               (DWORD)1,   // length of string
               coord,      // first cell to write to
               &cWritten); // actual number written
     }
}

// print string literally onto screen at specified position
void PrintXY(HANDLE hOutput, int x, int y, char *string)
{
     DWORD cWritten;
     COORD coord;

     coord.X = x;
     coord.Y = y;

     WriteConsoleOutputCharacter(
          hOutput,         // screen buffer handle
          string,          // pointer to source string
          lstrlen(string), // length of string
          coord,           // first cell to write to
          &cWritten);      // actual number written
}

hongjun
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6339932
The last parameter of the SetAttribute function can be of any of the below combinations.

FOREGROUND_RED
FOREGROUND_GREEN
FOREGROUND_BLUE
FOREGROUND_INTENSITY
BACKGROUND_RED
BACKGROUND_GREEN
BACKGROUND_BLUE
BACKGROUND_INTENSITY

To get white foreground colour and red background colour, use this combination.
SetAttribute(hOutput, 1, 1, strlen("Hello World"), FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|BACKGROUND_RED);

hongjun
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6347352
Any progress?

hongjun
0
 

Author Comment

by:kapai
ID: 6348110
Ran it, not sure about couple things but it does the job.
Compiler doesn't like the way you declare 'int i' then tried to use it in 'for' loop.
Double points if you can tell me how to change entire background color.
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 33

Accepted Solution

by:
hongjun earned 300 total points
ID: 6349276
>>change entire background color

Try this. It works for me. I am setting background colour to be bright yellow.

for ( i=0; i<=46; i++ )
{
     // setting foreground color of main menu
     SetAttribute(0, i, 100, BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_INTENSITY);
     for ( j=0; j<100; j++ )
          PrintXY(0, i, " ");
}

hongjun
0
 

Author Comment

by:kapai
ID: 6350622
Thanks, didnt realise they put an upper limit on points.
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6350823
Haha you could always create a question just for me if you wish to give more :)

hongjun
0
 

Author Comment

by:kapai
ID: 6356695
Dont worry got lots of questions
hehehe
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Picking random number 8 175
Performance degradation with linux shared library 6 135
How to organize data in excel ? 2 112
First character of input string truncated with scanf 3 94
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

29 Experts available now in Live!

Get 1:1 Help Now