Set a pixel...

This is a discussion on Set a pixel... within the C Programming forums, part of the General Programming Boards category; Hi, I'm new to these forums. Unfortunately my C skills are currently very limited, I've just started learning. I've checked ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    32

    Set a pixel...

    Hi, I'm new to these forums.

    Unfortunately my C skills are currently very limited, I've just started learning. I've checked through the FAQs, guidelines etc. and haven't found any (useful) information regarding this issue.

    Quite simply, I would like, preferably in a DOS window (Windows API confuses me) to set a pixel to a given colour. At the moment I'm not too worried if it will work on every machine in the world or just my one PC - as long as I can see something.

    If I can set a pixel, I'm happy. I can make my own functions to draw shapes, boxes, outlines, etc.

    This seems a remarkably complicated issue compared to Setpixel(x,y,hex) in other languages (Java/Javascript I tihnk). I've searched the forum and only found people talking about libraries and other things I did not really understand...

    Basically, if someone could just point me in the right direction with maybe an include/function/anything to plot a pixel on the screen.

    I did try using SetPixel() which came with some Windows library, but it seemed to require four parameters, and I couldn't figure out what the fourth one was meant to be.

    I found an example that draws random coloured lines over the screen but... it crashed my compiler (Bloodshed Dev C++ 5 beta).

    Umm, probably too long a message for such an issue but any ideas on the simplest, clearest, easiest way for a newbie to start drawing things on the screen?

    Thanks for any responses...
    - Tigs

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What you're asking is compiler specific, I'm afraid. I'd suggest looking round the web for info relating to your compiler and this topic.

    If you are having troubles with a specific piece of code, you could try posting it here. I'm not guaranteeing help, as I don't program graphics in DOS (well, Windows console), but there's probably someone here who would be able to point out what you're doing wrong.

    Good luck
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    Hi,
    try importing the Borland Graphics Interface file (graphics.h) from Turbo C or Borland C and then use the following code. I believe it should work on ur system under DOS.

    #include<graphics.h>
    int main()
    {
    int gd=DETECT,gm,x,y;
    /* gd=Graphics Driver detected automatically when u say DETECT and gm =Graphics Mode (default). */
    initgraph(&gd,&gm,"");
    x=100;
    y=100;
    putpixel(x,y,i); /* where i is any integer between 0-15 */
    getch();
    closegraph();
    }

    if u get a BGI initialization error then simply create a directory called BGI in ur current working directory.

    Get back if it does not work.

    Warm REgards,
    Sriharsha.
    Help everyone you can

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    32

    Setpixel again

    Sorry, I'm not trying to bump, I just got back from a week's holiday.

    Is it easier to set a pixel within a Windows window then? Not an easy to use, simple library Windows function to do it? I'll be happy to give it a try, just I'm more comfortable with DOS at the moment.

    I'll have a look round for Turbo C / Borland's graphics.h file then... hope I don't have to do more big downloads just for one header file

    Anyway, thanks for your help.
    - Tigs

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    32

    Set Pixel

    I found Graphics.h and all the library stuff needed, thats all alright. However, I tried your code Sriharsha, and the compiler gave a parse error in every line of graphics.h from line 240 to 370 (about 130 parse errors). Not good... any ideas?
    - Tigs

  6. #6
    Registered User whistlenm1's Avatar
    Join Date
    Jan 2002
    Posts
    124
    If I understand correctly what you are asking for, try the following link:

    http://www.brackeen.com/home/vga/
    Man's mind once streched by a new idea, never regains its original dimensions
    - Oliver Wendell Holmes

    In other words, if you teach your cat to bark (output) and eat dog food (input) that doesn't make him a dog. It would have to chase cars, chew bones, and have puppies before I'd call it Rover ;-)
    - WaltP

  7. #7
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Red face Hmmm...well...

    Well,
    if uve tried my code and it gave errors etc only indicates that the compiler does not support graphics.h library that you included. C, I did not give u the Standard code (ANSI), so its not guarenteed to work everywhere with every compiler. This code with the library works perfectly with Turbo C, in DOS environment. I believe there is No ANSI standard for graphics. So try other ways out as stated by others.

    Regards,
    Sriharsha.

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    32

    Arrow Thanks, but...

    Thanks for everyone's help so far... still got some problems though.

    Here is the code I'm using (entire file):

    Code:
    int main()
    {
      typedef unsigned char byte;
      byte far *VGA = (byte far*)0xA0000000L;
      VGA[16050] = 15;
      getch();
      return (0);
    }
    The Dev C++ compiler complains about VGA not being defined, first use in function, and won't compile. Turbo C accepts it and makes an EXE, but it doesn't show anything. I think this should end up with a white pixel at 50,50 - but nothing shows up at all...

    Thanks again, hope I can sort this out sometime...

  9. #9
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Wink May not be enough

    Hi Tigs,
    One problem I see is that you have

    First: The Supplied Address is wrong. The address for SVGA is 0xB8000000;

    Second: You have not turned on the graphics mode. So, you are using DOS mode (character mode 80 *25). Here each character space occupies 2 bytes. The first one for color and the other one for ASCII value of the character to be displayed (or the reverse, I need to refer). So only from 0 - 1999 values are available alternatively.

    Try this code instead...

    int main()
    {
    char far *vga = (char far *) 0xB8000000;

    int i;

    /* if it dosent work change the initial i to 1) */

    for (i=0;i<2000;i+=2)
    *vga=10;
    return 0;
    }

    Regards,
    Sriharsha.

  10. #10
    Registered User
    Join Date
    Jul 2002
    Posts
    32

    Question Blarg

    Thanks... but all I see (with Turbo C) is a single ASCII char in the top left... the other compiler gives syntax error on '*' and 'vga'. What was that code meant to do? Clear the screen?

    I tried pixel.c from whistlenm1's link, in both compilers. Neither could compile it.

    I've got the Allegro library, will that help for setting pixels?

    Sorry to be a bore, but if anyone has Dev C++ or Turbo C who could write a little function to set a pixel and test it...

    Hmm, I think I'll get MSVC some time.
    - Tigs

  11. #11
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    would allegro help? yes. allegro is a very easy to use graphics (among other things) library.
    hello, internet!

  12. #12
    Registered User
    Join Date
    Jul 2002
    Posts
    32

    Hmm

    Well, I have it, and I'm not entirely sure how to use it. I read the readme's, extracted it all ect. and DJGPP, Turbo C and Dev C++ can't compile anything with #include "allegro.h" in it. It goes on about missing files within allegro.h, and wont compile...

    If there's no easy way/example (which works)/code which can do it without being compiler specific all over, I'll give up. Jamascript (a new language I've been experimenting with) makes it so easy - SetPixel(50,50,0xFFFFFF); sets white at 50,50. Whereas C is all writing to memory or messing around in BIOS or using inlcudes which include non existant files or using drivers.

    Thanks for your time, everyone, but, maybe possibly, some day I'll manage this simple task... there's newbies for you.
    - Tigs

  13. #13
    Registered User
    Join Date
    Aug 2002
    Posts
    3
    I have to agree with Tigs on this one.
    In the past week I have installed DevC++(mingw), cygwin, lcc-win32, BCC5.5,
    and finally Visual C 6.0 trying in vain to set my graphics card into mode 13.
    Every compiler gives errors when using
    Code:
    union REGS regs;
    (apparenty I have no .h files that know what REGS is(every compiler))
    They each also blow up when trying to use inline asm to set the gfx mode.
    Visual C is the only compiler that seems to understand inline asm, but dies when getting to "int 10h;".
    I too am about ready to give up! (dont' make me go back to pascal!!)
    Installing Allegro was a soul sucking excercise in futility.
    i wasted about 5 hours trying to set that thing up (yes, i read the docs way too many times)
    Last edited by loggjam; 08-05-2002 at 05:32 PM.

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I did try using SetPixel() which came with some Windows library, but it seemed to require four parameters, and I couldn't figure out what the fourth one was meant to be.

    Here's a simple example.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <windows.h>
    
    int main(void)
    {
       int i, j;
       HWND window;
       HDC dc;
    
       printf("\n");
       window = GetActiveWindow();
       dc = GetDC(window);
       Rectangle(dc,0,0,100,100);
       for (i=0; i<100; i++)
       {
          for (j=0; j<100; j++)
             SetPixel(dc,i,j,RGB(255,0,0));
       }
       ReleaseDC(window,dc);
       return 0;
    }

  15. #15
    Registered User
    Join Date
    Aug 2002
    Posts
    2
    Hehe, I try

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find all possible subset of a given set
    By Downnin in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 02:03 PM
  2. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 01:34 PM
  3. Program help
    By Trebor in forum C++ Programming
    Replies: 4
    Last Post: 06-04-2002, 04:19 PM
  4. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM
  5. is there a way to set a pixel or change font color?
    By Flikm in forum C++ Programming
    Replies: 9
    Last Post: 09-01-2001, 02:24 PM

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