You are here:

C++/I have the following problem...

Advertisement


Question
I have the following problem :

i am trying to output to the screen a matrix of colored points, using windows xp. i downloaded the bloodshed c++ compiler but i am completely bogged down by the INSANE, COMPLICATED and EZOTERIC requirements to actually output graphics to a windows window.

what i am really trying to do is this : i have a mathematical model designed to estimate the evolution of a population as a function of each individual's environment. in my case the points in the matrix will be the individuals and the points around each point will be that point's environment. each point can have a certain color, coresponding to that point's state. that matrix is changed constantly to account for the evolution of the population.

i have some complicated problems to solve about that matrix that i will not bore you with. however it has occured to me it might be helpfull if i am able to visualize the matrix. i only have so much time in a day, i can either use it to do what i know how to do, namely the math behind all that modelling, or learn something that i dont know how to do, and dont feel very inclined to (namely to program graphics in windows)

if you or anyone else would be kind enough to supply me with a working source code in c++ that does the following : takes a bitmap from memory and outputs it in a window, and redraws that output every set interval, i will be very gratefull.

Answer
Hello rich, thanks for the question.

It takes a couple of steps to render a loaded bitmap on the screen. Also, when you say "redraw the output every set interval" I assume that means every frame. The following code sample should help you out. This is also assuming you successsfully created your window and got the handle to it previously:

// bitmap initialization
HDC hDC, hMemDC;
HBITMAP hBmp;
RECT screen;
HWND hWnd;

/* hWnd = CreateWindow(....) */

hDC = GetDC(hWnd);  

hMemDC = CreateCompatibleDC(hDC);

GetClientRect(hWnd, &screen);
     
hBmp = (HBITMAP)LoadImage(NULL, "NameOfBitmap.bmp", IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE);

// in case WM_PAINT:
hOldBmp = (HBITMAP)SelectObject(hMemDC, hBmp);

BitBlt(hDC, screen.left, screen.top, screen.right, screen.bottom, hMemDC, screen.left, screen.top, SRCCOPY);

That's all you need. Remember, we have to buffer everything to a MemDC so it you can't actually see it being drawn onto the screen. The BitBlt function then blits all the information on the memory DC to the main DC, effectively rendering the image. If by "every set interval" you didn't mean every frame, when you need to update the screen just call the function InvalidateRect(), which tells the screen to update itself manually.

I hope this information was helpful.

- Eddie  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.