Drawing pixels with C++

D.V.D

Make a wish
Reaction score
73
I've seen many functions in tutorials such as putpixel(...) but they do not specify which library in C++ gives you this function. I looked around at the Cplusplus.com library's and couldn't find a library that provides the user the ability to draw a pixel. I do not want to import a whole library with 3d capabilities and what not like OpenGL as I wanna try to make images and stuff like that from scratch. Any help?
 

Vestras

Retired
Reaction score
248
I've seen many functions in tutorials such as putpixel(...) but they do not specify which library in C++ gives you this function. I looked around at the Cplusplus.com library's and couldn't find a library that provides the user the ability to draw a pixel. I do not want to import a whole library with 3d capabilities and what not like OpenGL as I wanna try to make images and stuff like that from scratch. Any help?

I'm pretty sure putpixel is a part of allegro.
 

D.V.D

Make a wish
Reaction score
73

Intresting, but does that only support windows or does it work on linux/mac?

And would it be quicker to use Direct X instead of that windows library?

EDIT: I just saw a few video's showing how OpenGL is better than Direct X, only problem is I don't want to import a huge library filled with hundreds of functions when all I need is a SetPixel function, getpixel, and clear pixel.
 

Vestras

Retired
Reaction score
248
Intresting, but does that only support windows or does it work on linux/mac?

And would it be quicker to use Direct X instead of that windows library?

EDIT: I just saw a few video's showing how OpenGL is better than Direct X, only problem is I don't want to import a huge library filled with hundreds of functions when all I need is a SetPixel function, getpixel, and clear pixel.

SetPixel is Windows only, yes, but if you want it to be cross-platform, say you want to support Mac and Linux too, you can just make a wrapper for the pixel functions and then inside the wrapper funcs, based on the current platform, call the current platform's equivalent.

Also, many would argue that OpenGL isn't better than Direct X.
 

D.V.D

Make a wish
Reaction score
73
Would I need to know assembly or something for that? And are the only possibilities for me to use pixel functions are to use huge graphic library's like direct x and opengl? Cause ive been looking all over the internet but the only pixel functions available are from huge graphic and math library's.

Direct X seems to have advantages over OpenGl and OpenGl has its own performance advantages. The biggest difference however is that OpenGL is cross platform which is a huge advantage over Direct X which will only support windows.
 

Vestras

Retired
Reaction score
248
Would I need to know assembly or something for that? And are the only possibilities for me to use pixel functions are to use huge graphic library's like direct x and opengl? Cause ive been looking all over the internet but the only pixel functions available are from huge graphic and math library's.

Direct X seems to have advantages over OpenGl and OpenGl has its own performance advantages. The biggest difference however is that OpenGL is cross platform which is a huge advantage over Direct X which will only support windows.

No, not at all. You code just do something like: (pseoducode)
Code:
void draw_pixel(int x, int y, PIXEL pixel) {
#if WINDOWS
    SetPixel(...); // Windows pixel drawing function call
#elseif MAC
    draw_pixel(...); // Mac pixel drawing function call (not sure about the name)
// and so on...
#endif
}
 

D.V.D

Make a wish
Reaction score
73
Yeah but what would be the widows/mac draw pixel function calls if they don't exist in C++?
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
To elaborate on what Vestras wrote:

Code:
#ifdef _WIN32 // note the underscore: without it, it's not msdn official!
#include <Windows.h>
#elif __APPLE__  // Mac OS
#include <MacGraphics>
#endif

void PlaceAPixel ( int x, int y ){

#ifdef _WIN32
    SetPixel( x, y, blah );
#elif _APPLE__
    SetMacPixel( x, y, blah );
#endif

}

So you use preprocessor definitions to include the right libraries and write a wrapper for easier use.

Just remember that this is conditional compilation!
The program, if compiled on a Windows machine, will only work on Windows.
You need to compile it on a Mac in order to create a build that works on Macs.

The conditional definitions can only be used so you don't have to actually change anything in the source code when compiling it on a different machine.
 

D.V.D

Make a wish
Reaction score
73
If I understood correctly, by having conditions that tell the computer what library's to load, you will have to reinstall the program if you switch from mac to windows on the same computer?

And if I did use such a function, would I have to create my own C++ library that has the above code? Or is this done in another language? (pseudocode thing?)
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
If I understood correctly, by having conditions that tell the computer what library's to load, you will have to reinstall the program if you switch from mac to windows on the same computer?

And if I did use such a function, would I have to create my own C++ library that has the above code? Or is this done in another language? (pseudocode thing?)

About the first:
Reinstalling isn't exactly right. When you compile and build your exe with your IDE (say Visual Studio or GCC) you get a exe file which is built to be able to run on your type of system only. So if you compile on a Windows machine your program will import Windows.h and use Window's functions.
If you let someone compile your project on a Mac he'll get a exe (or whatever file they use lolz) that'll be made specifically for Mac.

That's why you can choose between downloading a Mac or Windows version of many programs in the internet.
When a user installs the program he just needs to make sure to get the right version of it. A Mac version won't work on a Windows PC.

And no, you don't have to write your own library. The Windows.h and MacGraphics library and standard libraries for Windows/Mac systems (I guessed the Mac name, I have no clue of Mac-specific programming). So they're existent from the start.
You just make a condition so the right libraries will be included during compilation.

That's quite a bit of organizational work actually. Since you probably don't have a Mac and a Windows at home to test both of the conditional branches.
And there's probably much more to it. If you're not using a library you need to use native commands to create a window to draw in (or you need to get the handle of the desktop if you want to draw onto there directly). These commands are most probably not the same for the two systems. So you'll probably have to rewrite quite a bit of code to have it work for different systems (that's why people generally use wrappers for that, so they don't have to rewrite all the stuff inside their main functions).
 

D.V.D

Make a wish
Reaction score
73
Hmm I see. Ill focus on PC right now as thats all I have now. I wanna learn how to get programs to work on both systems incase it comes handy in the future. Thanks for the help S3rius and Vestras :D +rep.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top