Rendering graphics in 2D
To render a 2D scene, we use the wireframe mode. This requires only the Line2D()
procedure to be implemented with the following prototype:
Line2D(int x1, int y1, int x2, int y2, int color);
Getting ready
This can be a simple implementation of the Bresenham’s algorithm (http://en.wikipedia.org/wiki/Bresenham’s_line_algorithm) and we do not present code here in the book to save space. See the accompanying Rendering.h
and Rendering.cpp
files for App4
. The book’s supplementary materials can be downloaded from www.packtpub.com/support.
How to do it…
To transform the objects from the simulated physical world to the screen in a 2D environment of the Box2D library, we have to set up a coordinate transform:
[x, y] [X_screen, Y_screen]
To do so, we introduce a few coefficients,
XScale
,YScale
,XOfs
,YOfs
, and two formulas:X_screen = x * XScale + XOfs Y_screen = y * YScale + YOfs
They work as follows:
int XToScreen(float x) { return Width / 2 + x * XScale + XOfs; } int YToScreen...