simple drawing in c++ ?

Started by
11 comments, last by TetsuoShima 19 years ago
Hello. I;m a new noob :) Well, first, thats a nice big community you have here :) glad to join it. I have this simple thing I want to do. I want to write a program that will draw my curves (functions) . What is the easy way to draw things in c++ ? I just want to have simple window with my drawn curve inside. Thanks in advance
Advertisement
C++ itself has no functions to do graphics renderings. You need to use some sort of graphics API ( application program interface ). If you are not concerned with speed you could probably just use the basic Win32 API. If you want to do something fancy and be fairly fast I would suggest looking up OpenGL or DirectX. Nehe has some good OpenGL tutorials. It depends more on what sort of curves you have and what/how you want to display them.
I want to be able drawing Bezier curves (or any other function). I have no problem with math, the problem is with programming. Actually, I want to code in c++ the same thing as "plot" function of MATLAB. Those are just simple black curves on white background.
Quote:Original post by acraig
C++ itself has no functions to do graphics renderings. You need to use some sort of graphics API ( application program interface ). If you are not concerned with speed you could probably just use the basic Win32 API. If you want to do something fancy and be fairly fast I would suggest looking up OpenGL or DirectX. Nehe has some good OpenGL tutorials. It depends more on what sort of curves you have and what/how you want to display them.


This win32 api is for windows programming, right? Is it necessary ?
And for "fancy" things, what will be easier to learn, DirectX or OpenGL .
If all you want to do is plot pixels, and you really want to do it in c++ then I would suggest picking up SDL or FreeGLUT. They're much easier to set up and get going than mucking around with Win32, and they're platform independent too.
Quote:Original post by JohnHurt
If all you want to do is plot pixels, and you really want to do it in c++ then I would suggest picking up SDL or FreeGLUT. They're much easier to set up and get going than mucking around with Win32, and they're platform independent too.


Best advice for what you want to do.
You could use SDL and SDL_gfx (since sdl by itself doesn't have line functions), I haven't personally used SDL_gfx but it should do what you want. There's probally an easier library somewhere. Although sdl is fairly easy and alot easier than the windows API, with the bonus of being cross platfrom. OpenGL and DirectX are a bit overkill for what your doing. But between those 2, I think opengl is a bit easier to learn (and I would recomend using SDL to setup OpenGL if you go that route)

If your using dev-cpp I could probally write up how to set up SDL and SDL_gfx, and a very minium program to make a window and draw a line and wait for you to exit. It should be understandable if you have a basic grasp of c/c++ (wouldn't take that long for me to write up).

If your using visual c++ the set up process for sdl is a bit harder (they have a step by step guide on sdl's web site though)
Alternatively, if you can accept offline drawing, you could easily create a PNM (PBM/PGM/PPM) image file writer (see wotsit.org for details).
Well I had some free time so I wrote up how to setup SDL_gfx in dev-c++, draw a black line on a white background.

Go to
http://www.bloodshed.net/dev/devcpp.html
and download
Dev-C++ 5.0 beta 9.2 (4.9.9.2) (9.0 MB) with Mingw/GCC 3.4.2
Install it, the default settings should be fine for you.
Run Dev-Cpp, The default settings should be fine again

Go to tools, Check for Updates/Packages
Click on check for updates
Select the WebUpdate Mirrors list
Click on Download selected, click ok.
Use select devpak server to go to the devpacks.org Community Devpaks
Check For Updates
Scroll through the list and select SDL (SDL needs to be installed before SDL_gfx)
Download selected
Go through the install. (click on the next and finished buttons)
Now select SDL_gfx
Download selected
Go through the install.

(Optionally Update Dev-C++ by Switching to the primary devpak server and downloading the dev-c++ update,
packman,binutils,gnu-debugger and what ever languages you use)

Go to new project, multimedia, and SDL_gfx example
Give your project a name.
Make a new folder and save your project there, now copy sample24.bmp and SDL_gfx.dll from dev-cpp\dll into
your project directory
And copy SDL.dll from dev-cpp\bin into your project directory
Compile and Run to see the example.

Since you wanted and to see how to do black lines on a white background I modified the original.
I left the Title author stuff Since I only changed a little bit.
//The has been modified(I should say gutted) //from the original that the below author wrote/******************************************************************************	Title:		SDL_gfx Example	Description:		This is an example of how to use the SDL_gfx DevKit.  After the dev kit		is installed into Dev-C++, you should be able to goto New Project and		see the SDL_gfx Example project available there.  SDL_gfx is a great		graphics library for SDL.  More information is available about it at		http://www.ferzkopp.net/~aschiffler/Software/SDL_gfx-2.0/	Author:		Rob Loach (http://www.robloach.net)	Date:		February 8th, 2005	Note:		REQUIRED - You must copy SDL.dll, SDL_gfx.dll and sample24.bmp		from Dev-C++/DLL directory to the folder where you saved this		project.		When making a new project, you must link to -lSDL_gfx.	\******************************************************************************/#include <SDL/SDL.h>#include <cstdio>#include <cstdlib>// SDL_gfx#include <SDL/SDL_gfxPrimitives.h>	// SDL_gfx Primitives#include <SDL/SDL_framerate.h>		// SDL_gfx Framerate Manager// For more information about SDL_gfx, please visit their website at// http://www.ferzkopp.net/~aschiffler/Software/SDL_gfx-2.0/int main(int argc, char *argv[]){		// Initialize SDL	if(SDL_Init(SDL_INIT_VIDEO) == -1){		fprintf(stderr, "Failed to initialize SDL: %s\n", SDL_GetError());		exit(1);	}	atexit(SDL_Quit);	// Initilize the screen	SDL_Surface *screen = SDL_SetVideoMode(400, 300, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);	if(screen == NULL){		fprintf(stderr, "Unable to set video mode: %s\n", SDL_GetError());		exit(1);	}        // FRAME RATE MANAGER EXAMPLE	// Initialize Frame Rate Manager	FPSmanager fpsm;	SDL_initFramerate(&fpsm);	SDL_setFramerate(&fpsm, 60); // 60 Frames Per Second	bool isRunning = true;	while(isRunning){	 		// Clear the screen to white, the 0xFFFFFFFF is the rgba written in hex		// 0xRRGGBBAA		SDL_FillRect(screen, NULL, 0xFFFFFFFF);                //the 0's are the location of the first point and the 300s are the         	//location of the second, then the last four numbers in rgba (from 0-255)                //a stands for alpha and it's used for how transparent the line is, 0                 //for completely see though 255 for opaque. if you like the shorter 	        //0xRRGGBBAA format you can use lineColor(screen,0,0,300,300,0x000000FF);       	        lineRGBA(screen,0,0,300,300,0,0,0,255);				 		// Output to the screen		SDL_Flip(screen);		// FRAME RATE MANAGER EXAMPLE		// Delay using the frame rate manager		SDL_framerateDelay(&fpsm);	 		// Check if user quits		SDL_Event event;		while(SDL_PollEvent(&event)){			switch(event.type){			case SDL_QUIT:				isRunning = false;				break;			case SDL_KEYDOWN:				if(event.key.keysym.sym == SDLK_ESCAPE)					isRunning = false;				break;			}		}		    }	    return 0;}


You'll want to look over the documentation for SDl and SDL_gfx. But for now basically just do all the drawing (or call the functions that do the drawing) between,
SDL_FillRect(screen, NULL, 0xFFFFFFFF);
and
SDL_Flip(screen);

Notes:
The picture is being drawn a maxium of 60 times per second, thats what the SDL Framerate stuff does. It's possible to draw it just once by puting SDL_FillRect and everything up to SDL_Flip before the while(isRunning) loop. I left it where it was so you can animate easily. You may want to look into timebased animation later, it's a little bit harder but you don't need to limit the framerate.

Also if you don't setup up your project from the template (the SDL_GFX example in the new projects menu) be sure to copy the settings in
Project Options, Parameters from the template
into your new project.

The screen resolution is the first 2 parameters to SDL_SetVideoMode.

All the SDL_Event stuff is to handle input, All it does now is check if your pushed the escape key, or if the os told it to quit. Clicking on the x button is one way that will cause the os can tell it to quit. If you want to add more input stuff check the SDL documentation.
Quote:Original post by Cocalus
Well I had some free time so I wrote up how to setup SDL_gfx in dev-c++, draw a black line on a white background.

Go to
http://www.bloodshed.net/dev/devcpp.html
and download
Dev-C++ 5.0 beta 9.2 (4.9.9.2) (9.0 MB) with Mingw/GCC 3.4.2
Install it, the default settings should be fine for you.
Run Dev-Cpp, The default settings should be fine again

Go to tools, Check for Updates/Packages
Click on check for updates
Select the WebUpdate Mirrors list
Click on Download selected, click ok.
Use select devpak server to go to the devpacks.org Community Devpaks
Check For Updates
Scroll through the list and select SDL (SDL needs to be installed before SDL_gfx)
Download selected
Go through the install. (click on the next and finished buttons)
Now select SDL_gfx
Download selected
Go through the install.

(Optionally Update Dev-C++ by Switching to the primary devpak server and downloading the dev-c++ update,
packman,binutils,gnu-debugger and what ever languages you use)

Go to new project, multimedia, and SDL_gfx example
Give your project a name.
Make a new folder and save your project there, now copy sample24.bmp and SDL_gfx.dll from dev-cpp\dll into
your project directory
And copy SDL.dll from dev-cpp\bin into your project directory
Compile and Run to see the example.

Since you wanted and to see how to do black lines on a white background I modified the original.
I left the Title author stuff Since I only changed a little bit.
*** Source Snippet Removed ***

You'll want to look over the documentation for SDl and SDL_gfx. But for now basically just do all the drawing (or call the functions that do the drawing) between,
SDL_FillRect(screen, NULL, 0xFFFFFFFF);
and
SDL_Flip(screen);

Notes:
The picture is being drawn a maxium of 60 times per second, thats what the SDL Framerate stuff does. It's possible to draw it just once by puting SDL_FillRect and everything up to SDL_Flip before the while(isRunning) loop. I left it where it was so you can animate easily. You may want to look into timebased animation later, it's a little bit harder but you don't need to limit the framerate.

Also if you don't setup up your project from the template (the SDL_GFX example in the new projects menu) be sure to copy the settings in
Project Options, Parameters from the template
into your new project.

The screen resolution is the first 2 parameters to SDL_SetVideoMode.

All the SDL_Event stuff is to handle input, All it does now is check if your pushed the escape key, or if the os told it to quit. Clicking on the x button is one way that will cause the os can tell it to quit. If you want to add more input stuff check the SDL documentation.


WOW. Thats a lot of help. I did exactly what you told me to do. It works perfect. I rated you as "extremely helpful", coz that's what you are :) . I thank you and everybody else.

This topic is closed to new replies.

Advertisement