c++ - Weirdly behaving pixel by pixel 3d graphing program [Faster drawing by dragging the window] -
so coding simple 3d graphing program in c++ using allegro 4 libraries. made in simple way, drawing pixel pixel. usually, putting single pixels on screen pretty slow because of how allegro works , plot graph in resolution of 640x480 have wait minute or two.
so rendering image buddy show , dragging windows around have screenshot , discovered dragging window rendering picture, speeds long hold window. 2 minutes draws in 10 seconds.
what's cause of bizzare behavior? related windows' windows or caused allegro itself? there explanation this?
also code
#include <allegro.h> #include <iostream> #include <math.h> using namespace std; float max_z = 1; float min_z =-1; float scale =50; inline void init(unsigned int width, unsigned int height) { allegro_init(); set_color_depth(24); set_gfx_mode(gfx_autodetect_windowed, width, height, 0, 0); install_timer(); install_keyboard(); install_mouse(); } inline void deinit() { clear_keybuf(); allegro_exit(); } int get_z_color (float z) { if (z >= 0) { return makecol(255, (z/max_z)*255, (z/max_z)*255); } else { return makecol(255 - (z/min_z)*255,0, 0); } } float get_z (float x, float y) { return sin(sqrt(pow(x,2)+pow(y,2))); } float int_to_float (int a) { return a; } int main() { unsigned int res_width, res_height; cout << "window size (w,h): "; cin >> res_width >> res_height; cout << endl << "initiating in " << res_width << "x" << res_height << " resolution..." << endl; init(res_width,res_height); cout << "success! drawing graph..." << endl; (int y=0; y<res_height; y++) { (int x=0; x<res_width; x++) { float valued_x = (int_to_float(x)-(int_to_float(res_width)/2))/scale; float valued_y = (int_to_float(-y)+(int_to_float(res_height)/2))/scale; _putpixel24(screen,x,y,get_z_color(get_z(valued_x,valued_y))); //cout << "drawing (" << valued_x << "," << valued_y << ")" << endl; } } cout << "graph drawn." << endl; cin >> new char; cout << "closing..."; deinit(); return 0; } end_of_main()
drawing on screen surfaces, whatever library you're using, costly operation.
i never used allegro (i'm using sdl), guess windows redraws screen surface each time single pixel put on it, , dragging windows around, prevent redrawing happening.
if want program dramatic increase in performance, should draw off-screen surface, , blit entire surface screen surface once drawing on (this basic double buffering technique).
like said, never used allegro, gather, :
int main() { unsigned int res_width, res_height; cout << "window size (w,h): "; cin >> res_width >> res_height; cout << endl << "initiating in " << res_width << "x" << res_height << " resolution..." << endl; init(res_width,res_height); bitmap *temporarybitmap = create_bitmap(res_width, res_height); cout << "success! drawing graph..." << endl; (int y=0; y<res_height; y++) { (int x=0; x<res_width; x++) { float valued_x = (int_to_float(x)-(int_to_float(res_width)/2))/scale; float valued_y = (int_to_float(-y)+(int_to_float(res_height)/2))/scale; _putpixel24(temporarybitmap,x,y,get_z_color(get_z(valued_x,valued_y))); //cout << "drawing (" << valued_x << "," << valued_y << ")" << endl; } } blit(temporarybitmap, screen, 0, 0, 0, 0, temporarybitmap->w, temporarybitmap->h); cout << "graph drawn." << endl; cin >> new char; cout << "closing..."; destroy_bitmap(temporarybitmap); deinit(); return 0; }
Comments
Post a Comment