GFX Programming
Probably, this question have been up a couple of million times, but lazy as I am, I havn't search the archives :/
I've been programming console programs and games enough to get tired of it now so I've decided to take the big step and start gfx programming..
Now to the question (drumroll): What should I start with? Which library?
BTW I've had an English test from 8:30 am to 1:00 pm.. So I'm quite tired.. Sory if you wil found anny spelling mistake
[edited by - Rimrok on May 14, 2002 10:24:07 AM]
The way to the right path is through pain, sweat and tears.
//******************************************************************************//*****Actual Function Implementaion Page for the mode 13h Graphics Library*****//******************************************************************************#include <dos.h>#include <math.h>//********************************Palette Defines for Mode 13h******************#define MODE13H 0x13#define TEXT_MODE 0x03#define PALETTE_MASK 0x3C6#define PALETTE_REGISTER_RD 0x3C7#define PALETTE_REGISTER_WR 0x3C8#define PALETTE_DATA 0x3C9//**********************GLOBAL VARIABLE DECLARATIONS****************************unsigned char far *video_buffer = (char far *)0xA0000000L; //Video memory is reserved for mode 13h.//Function Page Begins Below:void Video_Mode(int vmode) { asm mov ah, 0 asm mov al, BYTE PTR vmode asm int 10h }//******************************************************************************************void init_mode13h() { Video_Mode(MODE13H); }//******************************************************************************************void Plot_Pixel(int x, int y, unsigned char color) { video_buffer[((y<<8) + (y<<6)) + x] = color; }//******************************************************************************************void Set_PalReg(int index, unsigned char red, unsigned char green, unsigned char blue) { // Tell VGA card that a palette register will be updated outportb(PALETTE_MASK, 0xff); // Tell VGA card which register to update outportb(PALETTE_REGISTER_WR, index); // Write the red, green, and blue values for the specified index // to the VGA card (the same I/O port is used each time) outportb(PALETTE_DATA, red); outportb(PALETTE_DATA, green); outportb(PALETTE_DATA, blue); }//******************************************************************************************int Get_PalReg(int index, int color) { int r, g, b; outportb(PALETTE_MASK, 0xff); outportb(PALETTE_REGISTER_RD, index); r = inportb(PALETTE_DATA); g = inportb(PALETTE_DATA); b = inportb(PALETTE_DATA); if (color==1) return r; else if(color==2) return g; else if(color==3) return b; else return 0; }//******************************************************************************************void draw_line(int x1, int y1, int x2, int y2, int color) { int i; if(x1 == x2) { if(y1 < y2) for(i=y1; i <= y2; ++i) Plot_Pixel(x1, i, color); else for(i=y2; i <=y1; ++i) Plot_Pixel(x1, i, color); } else { double m, b; m = ((y2-y1)*1.0)/(x2-x1); b = y1 - m*x1; if(abs(x2-x1) > abs(y2-y1)) { // draw along x-axis if(x1 < x2) for(i=x1; i<=x2; ++i) Plot_Pixel(i, ((m*i)+b), color); else for(i=x2; i<=x1; ++i) Plot_Pixel(i, ((m*i)+b), color); } else { // draw along y-axis if(y1 < y2) for(i=y1; i<=y2; ++i) Plot_Pixel(((i-b)/m), i, color); else for (i=y2; i<=y1; ++i) Plot_Pixel(((i-b)/m), i, color); } } }//******************************************************************************************void draw_circle(int x, int y, int r, int color) { int i; double ty; for(i=-r; i<=r; ++i) { ty = sqrt(r*r - i*i); Plot_Pixel((x+i), (y+ty), color); Plot_Pixel((x+i), (y-ty), color); Plot_Pixel((x+ty), (y+i), color); Plot_Pixel((x-ty), (y+i), color); } }//******************************************************************************************void box_filled(int x1, int y1, int x2, int y2, int color) { if(y1 < y2) for(int i = x1; i < x2; ++i) for(int j = y1; j < y2; ++j) Plot_Pixel(i, j, color); else for(int i = x1; i < x2; ++i) for(int j = y2; j < y1; ++j) Plot_Pixel(i, j, color); }//******************************************************************************************void box_outlined(int x1, int y1, int x2, int y2, int color) { draw_line(x1, y1, x2, y1, color); draw_line(x1, y2, x2, y2, color); draw_line(x1, y1, x1, y2, color); draw_line(x2, y1, x2, y2, color); }//******************************************************************************************void box_in_box(int x1, int y1, int x2, int y2, int startc, int finishc) { if(startc < finishc) for(int currentc = startc; currentc <= finishc; ++currentc, --x1, --y1, ++x2, ++y2) box_outlined(x1, y1, x2, y2, currentc); else for(int currentc = startc; currentc >= finishc; --currentc, --x1, --y1, ++x2, ++y2) box_outlined(x1, y1, x2, y2, currentc); }//******************************************************************************************void black_screen() { box_filled(0,0,320,200,0); }//******************************************************************************************void erase_vert(int color) { int y1, y2; for(y1=0, y2=200; y1 <= 100; ++y1, --y2) { draw_line(0, y1, 320, y1, color); draw_line(0, y2, 320, y2, color); delay(10); } }//******************************************************************************************void erase_hori(int color) { int x1, x2; for(x1=0, x2=319; (x1 <= 160); ++x1, --x2) { draw_line(x1, 0, x1, 199, color); draw_line(x2, 0, x2, 199, color); delay(10); } }//******************************************************************************************void fade_to_red(int slowness) { int newred; for(int k=0; k <= 64; ++k) { for(int i=1; i <= 256; ++i) { if(Get_PalReg(i, 1) < 60) newred = Get_PalReg(i, 1) + 1; else newred = Get_PalReg(i, 1); Set_PalReg(i, newred, 0, 0); } delay(slowness); } box_filled(0,0,320,200,4); delay(slowness * 18); init_mode13h(); }
Include that in a project
//******************************************************************************//************************Header File for Wonder Workers''***********************//***********************Screen Mode 13h Graphics Library***********************//******************************************************************************//Function Declarations //Graphics Mode 13H Functions: void Video_Mode(int vmode); //preconditions: The screen mode for the program is in an unpredictable state // and can exist in any resolution that uses any palette of colors, // including mode 13h. //postconditions: The monitor and video card are adjusted to fit the "vmode" style. // The function usually receives one of the defined slots of memory // that are predefined above, such as "MODE13H" or "TEXT_MODE." void init_mode13h(); //preconditions: The screen mode for the program is in an unpredictable state // and can exist in any resolution that uses any palette of colors, // including mode 13h. //postconditions: The monitor and video memory are adapted to accompany 200 rows of // pixels by 320 columns of pixels by 256 colors. In other words, the // screen is set to mode 13h. void Plot_Pixel(int x, int y, unsigned char color); //preconditions: The video mode is set to version 13h, and the display on the screen // is in any condition. "color" is between 0 and 255, inclusively. // "x" should be between 0 and 319, inclusively, while "y" must be between // 0 and 199, inclusively. The program will not halt and errors will not // arise if the pixel''s coordinates are not within their specifications. //postconditions: The pixel of color "color" from the 256 various shades of the mode 13h // graphics library is planted in video memory at row "y" and column "x". void Set_PalReg(int index, unsigned char red, unsigned char green, unsigned char blue); //preconditions: The video mode is set to version 13h, and the display on the screen // is in any condition. The slot in the video buffer that is represented by // "index" exists and can adopt the new attributes of "red," "green," // and "blue." //postconditions: The red, green, and blue elements of the pixel with the address of // "index" in the video buffer have been altered with the new attributes // of "red," "green," and "blue," respectively. int Get_PalReg(int index, int color); //preconditions: The video mode is set to version 13h, and the display on the screen // is in any condition. "color" is either 1, 2, or 3, standing for the // red, green, or blue attribute of a pixel''s color, respectively. // The slot in the video buffer that is represented by "index" exists. //postconditions: If "color" is 1, then the function returned the red-value of the // pixel that has the address of "index" in video memory. However, the // green-value of the pixel at "index" is returned when "color" is 2, and // the blue attribute of the pixel is sent back to the caller if "color" // equals 3. //Basic Functions for the VGA Graphics Library void draw_line(int x1, int y1, int x2, int y2, int color); //preconditions: The video mode is set to version 13h, and the display on the screen // is in any condition. "color" is between 0 and 255, inclusively. // "x1" and "x2" should be between 0 and 319, inclusively, while "y1" // and "y2" must be between 0 and 199, inclusively. The program will // not halt and errors will not arise if the pixels'' coordinates are // not within their specifications. //postconditions: A line of color "color" stretches from the coordinate of (x1, y1) to // (x2, y2) in a step-like manner without smooth edges. The line has a // pixel length of one unit. void draw_circle(int x, int y, int r, int color); //preconditions: The video mode is set to version 13h, and the display on the screen // is in any condition. "color" is between 0 and 255, inclusively. // "x" should be between 0 and 319, inclusively, while "y" // must be between 0 and 199, inclusively. "r," which represents the // circle''s radius, should be at such a value that any point drawn "r" // pixels away from the shape''s center at (x, y) is still in the bounds of // the video mode. The program will not halt and errors will not arise // if the pixels'' coordinates are not within their specifications. //postconditions: A circle with a radius of "r" pixels and center at (x, y) is created // on the monitor in color "color." void box_filled(int x1, int y1, int x2, int y2, int color); //preconditions: The video mode is set to version 13h, and the display on the screen // is in any condition. "color" is between 0 and 255, inclusively. // "x1" and "x2" should be between 0 and 319, inclusively, while "y1" // and "y2" must be between 0 and 199, inclusively. The program will // not halt and errors will not arise if the pixels'' coordinates are // not within their specifications. //postconditions: A rectangle with an upper-left corner of (x1, y1) and a lower-right // corner of (x2, y2) is displayed on the monitor in color "color" and // then filled in with the color of "color." void box_outlined(int x1, int y1, int x2, int y2, int color); //preconditions: The video mode is set to version 13h, and the display on the screen // is in any condition. "color" is between 0 and 255, inclusively. // "x1" and "x2" should be between 0 and 319, inclusively, while "y1" // and "y2" must be between 0 and 199, inclusively. The program will // not halt and errors will not arise if the pixels'' coordinates are // not within their specifications. //postconditions: An empty, unfilled rectangle is placed on the screen in the shade of // "color." The coordinate of (x1, y1) represents the shape''s upper- // left corner, while (x2, y2) symbolizes the rectangle''s lower-right corner. void box_in_box(int x1, int y1, int x2, int y2, int startc, int finishc); //preconditions: The video mode is set to version 13h, and the display on the screen // is in any condition. "startc" and "finishc" are between 0 and 255, // inclusively. "x1" and "x2" should be between 0 and 319, inclusively, // while "y1" and "y2" must be between 0 and 199, inclusively. The // program will not halt and errors will not arise if the pixels'' // coordinates are not within their specifications. //postconditions: A cascading series of boxes that grow larger and shoot outwards is // formed on the monitor. The initial, most-inner rectangle that is not // filled has an upper-left coordinate of (x1, y1) and a lower-right corner // of coordinate (x2, y2). This starting rectangle is of color "startc," // and each successive box increments upwards by one color until surpassing // "finishc." In addition, every bigger box has an upper-left corner which // is one pixel to the left of and one pixel above the upper-left corner of // the previous rectangle. The lower-right coordinate of a new rectangle // is one pixel below and one pixel to the right of the similar corner // belonging to the previous rectangle. void black_screen(); //preconditions: The video mode is set to version 13h, and the display on the screen // is in any condition. //postconditions: The monitor is instantly erased with a sheet of black pixels. void erase_vert(int color); //preconditions: The video mode is set to version 13h, and the display on the screen // is in any condition. "color" is between 0 and 255, inclusively. //postconditions: A horizontal line in shade "color" and a second horizontal line of // color "color" began at the bottom and top of the monitor, respectively. // The two lines slowly moved towards the center row of the screen, // completing a solid display of "color" once they met. void erase_hori(int color); //preconditions: The video mode is set to version 13h, and the display on the screen // is in any condition. "color" is between 0 and 255, inclusively. //postconditions: A vertical line in shade "color" and a second vertical line of // color "color" began at the left and right of the monitor, respectively. // The two lines slowly moved towards the center column of the screen, // completing a solid display of "color" once they met. void fade_to_red(int slowness); //preconditions: The video mode is set to version 13h, and the display on the screen // is in any condition. "slowness" represents how slow the screen should // fade to red, with a value of 1 being extremely fast and the amount of // 100 being quite slow. The variable should be a positive integer. //postconditions: The entire screen began in its original condition and turned to a solid // sheet of color #4 (dark red) with a quickness corresponding to how // low "slowness" is. The red attribute of each color is intensified, while // the green and blue elements of every pixel are dropped to 0. The original // palette of colors is refreshed by re-initializing mode 13h.
And that as the header.
AHHH!!! I DID SOMETHING TO MY HEAD!
quote:
Original post by Rimrok
Probably, this question have been up a couple of million times, but lazy as I am, I havn''t search the archives :/
I''ve been programming console programs and games enough to get tired of it now so I''ve decided to take the big step and start gfx programming..
Now to the question (drumroll): What should I start with? Which library?
BTW I''ve had an English test from 8:30 am to 1:00 pm.. So I''m quite tired.. Sory if you wil found anny spelling mistake
[edited by - Rimrok on May 14, 2002 10:24:07 AM]
IMO there are two things you need:
1) Get a grip on the basics of the OS, especially the GUI. If you''re using windows, that''s the Win API. You don''t need to become an expert in it, just the basics for a beginning
2) Choose a graphics API that fits your taste. The main two are DirectX and OpenGL. There is a good article here on GameDev that compares the two.
Forever trusting who we are
And nothing else matters - Metallica
Forever trusting who we areAnd nothing else matters - Metallica
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement