need help in making graphics in C++

This is a discussion on need help in making graphics in C++ within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Everytime I try a method to make sprites in my game, it comes up with errors. I am using Bloodshed's ...

  1. #1
    Unregistered
    Guest

    Question need help in making graphics in C++

    Everytime I try a method to make sprites in my game, it comes up with errors. I am using Bloodshed's Dev C++ 4.01.

    It always comes up with errors in assember code. I tried to use the asm thing, and it came up with the same error. I tried the one on the faq, didn't work.

    I tried Allegro, that didn't work.

    I know it should be able to work, since it uses MingW, a nice compiler.


    Can someone just explain step by step how to set the screen to 13h mode? Please don't just drop some code, explain to me please!

    Thanks!

  2. #2
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    Okay, I have never used Dev C++, but if it supports DOS then this code should work. I will show you the code and then explain it to you:

    Code:
    unsigned char *Screen = (unsigned char *) 0xA0000000;
    
    void initGraph (void)
    {
        asm {
            mov ax, 13h
            int 10h
        }
    }
    
    void finishGraph (void)
    {
        asm {
            mov ax, 03h
            int 10h
        }
    }
    Okay, look at the first line where I declare screen.

    You have to declare your screen somewhere, and the address of the screen buffer in memory is 0xA0000000 (hexidecimal). So you say:

    unsigned char *Screen = (unsigned char *) 0xA0000000;

    why do you declare it as an unsigned char *? Because 13h is a 256 color mode...and unsigned char ranges from 0 to 255. So you declare a variable called Screen and give it the address of the screen so you can write directly to it.

    Next you have the initGraph function which initializes (starts) 13h graphics mode:

    void initGraph (void)
    {
    asm {
    mov ax, 13h
    int 10h
    }
    }

    To understand this code you have to understand assembler. The command int in assembler means interrupt. It is kind of like calling a function in C++. Each interrupt has parameters, like functions in C++. interrupt 21h (h is for hexidecimal) is the command to output to the screen. So if I did this:

    mov dl, 2
    mov ah, 0
    int 21
    int 20

    That would output a single character, a smiley face. Why? Because int 21 takes the two parameters, the registers dl and ah, and looks at them. dl tells it what to do. If dl is 2, then it means to output a single character. Then it looks at ah for what to output. It outputs the character 0. Then int 20 is like return 0 in C++.

    So lets apply this knowledge to the 13h asm code. interrupt 10h is the interrupt to change screen modes. It takes the parameter ax and looks at. The parameter contains 13h, so it knows to go to mode 13h.

    Now lets look at the finishGraph function:

    void finishGraph (void)
    {
    asm {
    mov ax, 03h
    int 10h
    }
    }

    This uses interrupt 10 again, and calls mode 3. Mode 3 is a text mode, so basically when we call this function we are exiting 13h and going back to a normal text mode.

    So how do we implement all of this code? Well, look at this small example:

    int main (void)
    {
    initGraph();

    Screen[0] = 3;

    getch();
    finishGraph();
    return 0;
    }

    This makes the pixel on the top left corner of the screen a kind of turquoise color. Now, why did I do that to draw to the screen?

    When we did this:

    unsigned char *Screen = (unsigned char *) 0xA0000000;

    We created a pointer called Screen which pointed to an unsigned char. Now, 0xA0000000 is the address of the screen, and it is an array of 64000 unsigned chars. The screen is linear. Each row of the screen is 320 pixels, and there are 200 rows, creating a total of 64000 pixels. So if we wanted to plot a pixel to the second row, first pixel on the row, we would say Screen[320] = color. Why would we say 320? Because the first row goes from 0 to 319, and the second row STARTS on 320 and goes to 639, etc.

    So that is a basic lesson on the screen in 13h. I will give you one more function. This will allow you to plot to the screen in (x,y) terms so you dont have to bother with all that Screen[xxx] = color stuff:

    Plot (int x, int y, unsigned char color, unsigned char *buffer)
    {
    buffer[(y<<8)+(y<<6)+x] = color;
    }

    To use this function:

    Plot(0, 0, 3, Screen);

    That would make the pixel in the top left hand corner the 3rd color, which is a kind of turquoise i think.

    Hope all of this helps!
    My Website

    "Circular logic is good because it is."

  3. #3
    Xterria
    Guest
    Hey, I did all the stuff you told unregistered. my compilers aren't liking the asm stuff. for example:
    'undefined symbol 'mov''

    please help
    thanks

  4. #4
    Xterria
    Guest
    nevermind typo! thanks for the tut..I am happy you have pleased the world want a carrot

  5. #5
    Xterria
    Guest
    ok..there IS a problem. I can compile it, but when i run it no pixels are plotted. it's all black. Please help

  6. #6
    WebSnozz
    Guest

    Lightbulb

    Maybe if it's a pixel on the edge of the screen and your monitor settings are off some it may be off the edge. Just a thought.

  7. #7
    Unregistered
    Guest

    Angry

    It says Parse Error before { a number of times and something about the hexadecimals aren't hexadecimals.

  8. #8
    Disagreeably Disagreeable
    Join Date
    Aug 2001
    Posts
    711
    I hate parse errors *spits on parse errors*

  9. #9
    Unregistered
    Guest

    Angry

    I fixed the hexadecimal part now I just get Parse error before { or if I try asm before every line parse error before the word after asm.



    AAAAAAHHHHHHHHHHHHHHHHHHHH!!

  10. #10
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Try Windows programming!

  11. #11
    DutchStud
    Guest
    When I try it, I don't get any errors, but there are not pixels on the screen. I tried changing the position of the pixels, so it's not a faulty screen configuration. Is there someway to tell the screen to update or something of that nature?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Turtle Graphics, how does it work?
    By freddyvorhees in forum C++ Programming
    Replies: 15
    Last Post: 08-28-2009, 09:57 AM
  2. Making control...
    By Finchie_88 in forum C++ Programming
    Replies: 2
    Last Post: 09-07-2004, 01:42 PM
  3. Graphics in the console (for real)
    By harryP in forum C++ Programming
    Replies: 11
    Last Post: 10-16-2003, 04:54 PM
  4. graphics programming idea
    By iain in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 01-18-2002, 07:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21