Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

BigChris

Beginning graphics?

This topic is 6620 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, as a beginning programmer (who has currently not moved out of regular dos ansi) I was wondering if there are any beginning graphics site''s/tutorials, for adding VGA graphics into DOS C programs, without use of Windows or a graphics library, just basic stuff so I can learn how they work. Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
Hmm, I''m not sure what your question is, could you please give a more detailed explanation of what you wanna do? Do you mean compiled graphics? or what?

Share this post


Link to post
Share on other sites
Sorry if I wasn''t clear. I just want to start doing graphics in my programs and have no idea how to start. Basically I want to start making games

Share this post


Link to post
Share on other sites
Hi BigChris,

Sorry, Dude i dont have any links to Tut... but search for VLA maybe you'll find some...

But here the basic ...

The text memory is located at segment (B800), the video segment is at (A000). So to simply draw something on the screen you need a pointer to that segment. But first of all you must switch to video card to a Video Mode. To do so, use mode 13h. Its the VGA standart mode. (320x200x8bits - 256 colors)


    

typedef unsigned char byte;
typedef unsigned int word;
typedef unsigned long dword;

typedef enum
{
none = 0x00,
text80x25 = 0x03, //or maybe 80x24 (dont remember)
vga320x200 = 0x13
}VGAMODE;


byte far *VideoMem = (byte far*)A0000000L;
byte far *DblBuffer = NULL;


void ChangeMode (VGAMODE NewMode)
{
__volatile__ __asm__
{
push ax
mov ax, NewMode
int 0x10
pop ax
}
}


void SetPixel (int x, int y, byte color)
{
// Find the offset, by multiplying 'Y' by Width
// Same as : (y * 320) + x
VideoMem [ (y >> 6) + (y >> 8) + x ] = color;
}


void InitDblBuffer (void)
{
DblBuffer = new byte [320 * 200];
}


void DestroyDblBuffer (void)
{
if (DblBuffer) delete DblBuffer;
}


void SwapBuffer (void)
{
if (DblBuffer) return;

// We will swap 4 pixel / iteration

dword *PtrDest = (dword *)VideoMem;
dword *PtrSrc = (dword *)DblBuffer;

for (dword i = 0; i < ((320 * 200) / 4); i++, PtrMem++, PtrSrc++)
PtrDest<i> = PtrSrc[i];
}


void ClearBuffer (byte far* Mem)
{
// We will clear 4 pixel / iteration

dword *PtrMem = (dword *)Mem;

for (dword i = 0; i < ((320 * 200) / 4); i++, PtrMem++)
PtrMem[i] = 0;
}




With that you will be able to get a feel from VGA programming...
And it's all come directly from my head so maybe you will need to change some piece of code. But i think it will compile without any problem in BC. But if you use DJGPP, it won't work, sorry a little more complex 'cause DJGPP use Protected-Mode ... So keep that for a latter day.

Happy Coding ...

LowRad

Edited by - LowRad on June 26, 2000 4:08:41 PM

Share this post


Link to post
Share on other sites
I don''t know how well you know C/C++(even ASM & Pascal) but if you don''t know at least C/C++ then that''s the first think you gotta know before you start... anyways.. here''s some code that would put a pixel at the center of the screen in mode 13h (320x200)

    
#include <conio.h>

// define the video modes constants

#define VGA_MODE 0x0013
#define TEXT_MODE 0x0003

// define a BYTE type

typedef unsigned char byte;

// declare a pointer that points to the video memory which is

// at A000:0000 (this declaration is specifically for TC++
byte far* vga = (byte far *)0xA0000000L;

// This functions sets the mode while you''re in DOS

// you just pass the video mode constant to this function and

// it sets it for you...

// btw, INT in the ASM code stands for interrupt (like a function
// call in C... and the function parameter is move to AX which

// is one of the PC''s registers used for calculation and also

// used with interrupts just like we do in the sample code

void SetMode(unsigned short mode)
{
asm {
mov ax, [mode]
int 10h
}
}

// The following functions places a pixel on the screen

// As you already know mode13h has a resolution of 320x200

// which is 320-width and 200-height... Since the VGA screen is

// a single dimension (opposed to 2D, which would use
// a 2D array), it uses an array to access all of it''s elements
// If you know how a pointer works in C/C++ you''d know what''s

// going on in the code below. In order to place a pixel at the

// correct location, you have to multiply the Y value by
// the width (320), and then add the X value. Think about it.
// Since it''s a 1D array, and there are 320 pixels in each row,
// you have to multiply the Y value you wanna get to by 320

// and then add the additional X value.. pretty easy.. It''s gonna

// take some time to get used to it

void PutPixel(int x, int y, byte color)
{
vga[y*320+x] = color;
}

int main()
{
// Go into Mode 13h, which is the VGA
SetMode(VGA_MODE);
// Use out function to put a pixel at location X=160, Y=100,
// with Color=15 (WHITE) which is already defined for. Well,
// it''s defined until you change it, which you''ll learn about
// later on.

PutPixel(160, 100, 15);
// Wait for a keypress

getch();
// Go back to text mode

SetMode(TEXT_MODE);
// Exit the program

return 0;
}


Hopefully you''ve learned something from my ranting... If you need help with anything just ask... before I go

..-=ViKtOr=-..

Share this post


Link to post
Share on other sites
LowRad, the code below

VideoMem [ (y >> 6) + (y >> 8) + x ] = color;

should read


VideoMem[ (y << 8) + (y << 6) + x ] = color;

Anyways...

Share this post


Link to post
Share on other sites
Thanks for taking the time to answer guys, much appreciated.

Gladiator, I get the idea now from your code, thanks a lot. Only prob, it wouldn't compile in DJGPP, is that a djgpp error? I got:
undefined ref. to main and
Error: collect2: ld returned 1 exit status

It's probably something stupid that I did wrong, but I cant see it

I am pretty new to programming (about a week of learning c from a book) but know most of the basics now, I just wanted to see how I would start in vga mode. Do you know of any good books of beginning graphics?

Thanks again fellas...

Chris


Edited by - BigChris on June 26, 2000 4:39:51 PM

Share this post


Link to post
Share on other sites
Chris,

As i told you DJGPP use Protected Mode. It''s a 32bits compiler.
In Real-Mode, the normal mode of operation of DOS. An address is specified by parring a SEGMENT:OFFSET, which Segment is the base adress and the offset is the offset from that segment. (hmmmm... i think an exemple is required) If i do - A000:0010 = 15;

This line will put a White Pixel ''15'' to the 10th pixel on the first row of your screen. A000:320 = 15; will put a White Pixel to the first pixel on the second row.

For the protected-mode interface, like DJGGP, you use a SELECTOR:OFFSET, which is a little different and a lot more difficult to work it. Start in Real-Mode or jump right away to windows!

Just hope its help...

LowRad

Share this post


Link to post
Share on other sites
LowRad:

I see. Thanks for explaining. I think I may just start with Windows then, instead. Would you reccomend Direct X (I guess I''d have to use that in Windows!)? I have noticed there are a few of ''beginner'' books/net tutorials around for that. Thanks.

Chris

Share this post


Link to post
Share on other sites
Here is a DJGPP example off the top of my head..

    
#include <conio.h>

#define VGA_MODE 0x0013
#define TEXT_MODE 0x0003

typedef unsigned char byte;

byte* vga = (byte *)0xA000;

void SetMode(unsigned short mode)
{
REGS regs;

regs.ax = mode;
int86(0x10, ®s, ®s);
}
void PutPixel(int x, int y, byte color)
{
vga[y*320+x] = color;
}

int main()
{
SetMode(VGA_MODE);
__djgpp_enable_nearptr();
vga += __djgpp_conventional_base;
PutPixel(160, 100, 15);
getch();
SetMode(TEXT_MODE);
__djgpp_disable_nearptr();
return 0;
}


I'm not exactly sure about the __djgpp.... naming of the functions, but it would be something similar. Search for some tuts on the net... Try www.yahoo.com and www.av.com..

Oh and in order to use the REGS structure you gotta add an additional header file, but i forgot the name... I think it's stdlib.h... not sure... later!

..-=ViKtOr=-..

Edited by - Gladiator on June 26, 2000 4:58:06 PM

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!