• Advertisement

Archived

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

Beginning graphics?

This topic is 6442 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
Chris,

The Base for Graphics in Windows is GDI, probably you have already see that word. Its slow, and inefficient. From that base microsoft''s programmers have develop MFC which is simply classes using GDI to draw windows, button, messagebox,...

When its come to Game Programming, use DirectX or any other API for Windows... DirectX include Direct3D too...

While you seem to dont known much about programming... Its will not be so hard to start from windows !!

Check for some docs here on GameDev for Win32, DirectX, ...

You will see ...

Happy Coding

LowRad

Share this post


Link to post
Share on other sites
2Gladiator, you should replace
byte* vga = (byte *)0xA000;
with
byte* vga = (byte *)0xA0000;

just to be sure you''re giving right and working example

and to BigChris, i suggest you to examine examples from djgpp or even get an Allegro library if for some reason you''ll stay with dos

and btw, what ubb tags you guys using to place code into that kind of white box?

Regards,
FlyFire/CodeX
http://www.codex.nm.ru

Share this post


Link to post
Share on other sites
You can check http://www.flipcode.com

There are plenty of nice stuff, including how to build a 3d portal engine in software (DOS).



-* So many things to do, so few time to spend *-

Share this post


Link to post
Share on other sites
Buy a book!
for $50 you can get LaMothe''s lastest & greatest treatise on Dx called "Tricks of the Windows Game Programming Gurus" A demo version of MS VC++ 6 comes on the second of the two CD''s that come with it

If you have never written a windows or directX program before it''ll get you started

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If you want a tutorial on VGA programming (320x200x256) including Mode X and source code that compiles in DJGPP, go here:


http://www.brackeen.com/home/vga/

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I say you use a library. Sure you can learn how to do it with assembly language, like many people are showing here. The problem is, that just isn''t useful. The code will not be portable, and it will not help you understand modern computer graphics. Modern computer graphics are constructed with an API such as OpenGL -- nobody uses assembly language for graphics anymore. (well maybe a few crazy fins)

Assembly language is still useful for optimizations don''t get me wrong -- but it''s not used for initializing video modes, plotting pixels, etc. (Actually, i''m not talking about assembly so much as I''m talking about direct writes to video memory, and calling video bios functions directly).

I suggest you download the simple direct media layer library - which allows you to write portable code to a variety of platforms. You can write a windows full screen (or windowed app) without having to worry about win32, mfc, etc... The latest release supports OpenGL as well.

It''s a simple, minimal library to use, and I much prefer it to writing DirectX code by hand. (To be honest, I''ve never tried this. Nor do I particularly want to, since it seems like a sure-fire way to make non portable software)



quote:
Original post by BigChris

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
quote:
Actually, i'm not talking about assembly so much as I'm talking about direct writes to video memory, and calling video bios functions directly



Really? I thought for a second that you didn't know what you're talking about... ASM is still useful, and it will always be. So stop ranting and... I dunno... go take cold shower...


FlyFire, thanks for fixing that mistake... I forgot to change that...

btw, you mean to tell me that I got the correct function names (__djgpp_xxx ones ) from the first time? Wow... Kewl...

..-=ViKtOr=-..

Edited by - Gladiator on July 5, 2000 12:39:42 PM

Share this post


Link to post
Share on other sites
BigChris,

Freshen up on linear algebra and matrices, and then head on over to The OpenGL Homepage!

Its a very cool API for 3D graphics and I think is great for begginers as long as you know C (and a little math ).

Edited by - Zipster on July 6, 2000 5:26:01 AM

Share this post


Link to post
Share on other sites
BigChris,

I think I know what you mean. If you want to stick to a DOS environment and get the hang of C and a basic graphics library, and easy and quick way is to get a copy of Turbo C v1.5 or v2.0 for DOS. It comes with a couple examples. There are other compiler options out there, but you can pick up Turbo C for free at the Borland/Inprise "museum".

Just go to http://community.borland.com/museum/

// CHRIS

Share this post


Link to post
Share on other sites

  • Advertisement