Tutorial - Mode13H
Author:Arkon
Category:DOS Graphics
Views:6975
Mode13h is a screen resolution of 320x200 pixels with 256 colors (8bit color depth). This means each pixel 'takes' one byte (8bits, 0 - 255).
The screen contains (320x200=)64000 pixels. These 64000 pixels are stored in the Video Memory.
The video card reads the data from the Video Memory and sends it to the Monitor automatically.
You access the Video Memory as an array of 64000 bytes.
The Video Memory starting address (segment) is 0xA000 (0x means Hex), this value is constant in every computer and allocated for the Video Memory.

First you need to change the screen resolution to Mode 13h.

 ```void init_mode13h() { asm { mov ax, 0x13 // We set ax to our mode number (13h) int 0x10 // We tell the BIOS to switch mode } } ```

The basic of drawing anything on the screen is drawing a PIXEL.
To draw a pixel by X,Y coordinates you need to calculate its position (offset) on the Video Memory:
You see the screen as a 2D surface, but in the Video Memory it is stored as a 1D array, like a line (linear).
For example: The first row starts at offset 0, all the pixels of the first row are stored from 0 to 319, the second row starts at offset 320 and ends at offset 639 and so on...
The Monitor passes a row every 320 pixels.
Therefore, the formula of getting to the offset of a certain row is: Y*320 and to get the pixel's offset we just add the column number: Y*320+X
We just treat the screen as a 2D array for comfort.

Here is an example of a coordinate on the screen.
Its coordinates: X = 3, Y = 4, Color = RED:

Here is the offset of that coordinate in the video memory:

Put Pixel:

 ```unsigned char *video_memory = (unsigned char *) 0xA0000000L; void put_pixel(int x, int y, unsigned char color) { video_memory[y * 320 + x] = color; } ```

 ```void init_textmode() // 80x25 Chars with 16 colors { asm { mov ax, 0x3 // We set ax to textmode's number (3) int 0x10 } } ```

Source Code:
DJGPP - mode13h.c
Turbo C - mode13h.cpp

 [1] goldrunescape | 2013-06-13 05:22:47unsigned char *video_memory = (unsigned char *) 0xA0000000L; void put_pixel(int x, int y, unsigned char color) {  video_memory[y * 320 + x] = color; http://www.d3joy.com/
NOTE:
Comments that will hurt anyone in any way will be deleted.