| 1 |
#include <ClanLib/application.h> |
| 2 |
#include <ClanLib/core.h> |
| 3 |
#include <ClanLib/display.h> |
| 4 |
#include <ClanLib/gl.h> |
| 5 |
#include <GL/gl.h> |
| 6 |
|
| 7 |
#define PIXELSIZE 3 |
| 8 |
|
| 9 |
///////////////////////////////////////////////////////////////////////////// |
| 10 |
// Application class |
| 11 |
|
| 12 |
class Application : public CL_ClanApplication |
| 13 |
{ |
| 14 |
public: |
| 15 |
int main(int argc, char** argv) |
| 16 |
{ |
| 17 |
CL_ConsoleWindow debug_console("Debug console"); |
| 18 |
debug_console.redirect_stdio(); |
| 19 |
|
| 20 |
try |
| 21 |
{ |
| 22 |
CL_SetupCore::init(); |
| 23 |
CL_SetupDisplay::init(); |
| 24 |
CL_SetupGL::init(); |
| 25 |
|
| 26 |
CL_DisplayWindowDescription desc; |
| 27 |
desc.set_title("ClanLib DrawPixels"); |
| 28 |
desc.set_size(CL_Size(1024, 768)); |
| 29 |
desc.set_bpp(32); |
| 30 |
desc.set_fullscreen(true); |
| 31 |
CL_DisplayWindow window(desc); |
| 32 |
|
| 33 |
flatten_render_pipeline(); |
| 34 |
|
| 35 |
width = CL_Display::get_width(); |
| 36 |
height = CL_Display::get_height(); |
| 37 |
|
| 38 |
image = (unsigned char*)malloc(width * height * PIXELSIZE); |
| 39 |
|
| 40 |
fill_buffer(); |
| 41 |
|
| 42 |
while (!CL_Keyboard::get_keycode(CL_KEY_ESCAPE)) |
| 43 |
{ |
| 44 |
update(); |
| 45 |
draw(); |
| 46 |
|
| 47 |
window.flip(); |
| 48 |
CL_System::keep_alive(); |
| 49 |
} |
| 50 |
|
| 51 |
CL_SetupGL::deinit(); |
| 52 |
CL_SetupDisplay::deinit(); |
| 53 |
CL_SetupCore::deinit(); |
| 54 |
} |
| 55 |
catch (CL_Error error) |
| 56 |
{ |
| 57 |
std::cout << "Uncaught error: " << error.message.c_str() << std::endl; |
| 58 |
} |
| 59 |
|
| 60 |
debug_console.wait_for_key(); |
| 61 |
return 0; |
| 62 |
} |
| 63 |
|
| 64 |
void flatten_render_pipeline() |
| 65 |
{ |
| 66 |
glDisable(GL_ALPHA_TEST); |
| 67 |
glDisable(GL_BLEND); |
| 68 |
glDisable(GL_DEPTH_TEST); |
| 69 |
glDisable(GL_DITHER); |
| 70 |
glDisable(GL_FOG); |
| 71 |
glDisable(GL_LIGHTING); |
| 72 |
glDisable(GL_LOGIC_OP); |
| 73 |
glDisable(GL_STENCIL_TEST); |
| 74 |
glDisable(GL_TEXTURE_1D); |
| 75 |
glDisable(GL_TEXTURE_2D); |
| 76 |
glPixelTransferi(GL_MAP_COLOR, GL_FALSE); |
| 77 |
glPixelTransferi(GL_RED_SCALE, 1); |
| 78 |
glPixelTransferi(GL_RED_BIAS, 0); |
| 79 |
glPixelTransferi(GL_GREEN_SCALE, 1); |
| 80 |
glPixelTransferi(GL_GREEN_BIAS, 0); |
| 81 |
glPixelTransferi(GL_BLUE_SCALE, 1); |
| 82 |
glPixelTransferi(GL_BLUE_BIAS, 0); |
| 83 |
glPixelTransferi(GL_ALPHA_SCALE, 1); |
| 84 |
glPixelTransferi(GL_ALPHA_BIAS, 0); |
| 85 |
|
| 86 |
#ifdef GL_EXT_convolution |
| 87 |
glDisable(GL_CONVOLUTION_1D_EXT); |
| 88 |
glDisable(GL_CONVOLUTION_2D_EXT); |
| 89 |
glDisable(GL_SEPARABLE_2D_EXT); |
| 90 |
#endif |
| 91 |
|
| 92 |
#ifdef GL_EXT_histogram |
| 93 |
glDisable(GL_HISTOGRAM_EXT); |
| 94 |
glDisable(GL_MINMAX_EXT); |
| 95 |
#endif |
| 96 |
|
| 97 |
#ifdef GL_EXT_texture3D |
| 98 |
glDisable(GL_TEXTURE_3D_EXT); |
| 99 |
#endif |
| 100 |
|
| 101 |
glPixelStorei(GL_PACK_ALIGNMENT, 1); |
| 102 |
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 103 |
} |
| 104 |
|
| 105 |
void update(void) |
| 106 |
{ |
| 107 |
// Calculate time since last update |
| 108 |
static int start_time = CL_System::get_time(); |
| 109 |
int cur_time = CL_System::get_time(); |
| 110 |
int delta_time = cur_time - start_time; |
| 111 |
start_time = cur_time; |
| 112 |
calc_fps(delta_time); |
| 113 |
|
| 114 |
// fill_buffer(); |
| 115 |
} |
| 116 |
|
| 117 |
void fill_buffer() |
| 118 |
{ |
| 119 |
// Fill screen with some lame pattern |
| 120 |
static char current_colour = 0; |
| 121 |
current_colour++; |
| 122 |
for(int i=0;i<width*height*PIXELSIZE;i++) |
| 123 |
image[i] = current_colour++; |
| 124 |
} |
| 125 |
|
| 126 |
void calc_fps(int frame_time) |
| 127 |
{ |
| 128 |
static int fps_counter = 0; |
| 129 |
static int total_time = 0; |
| 130 |
|
| 131 |
total_time += frame_time; |
| 132 |
if(total_time >= 1000)// One second has passed |
| 133 |
{ |
| 134 |
std::cout << fps_counter << std::endl; |
| 135 |
fps_counter = total_time = 0; |
| 136 |
} |
| 137 |
else |
| 138 |
fps_counter++;// Less than one second, increase fps |
| 139 |
} |
| 140 |
|
| 141 |
void draw(void) |
| 142 |
{ |
| 143 |
glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, (GLvoid*)image); |
| 144 |
} |
| 145 |
|
| 146 |
unsigned char* image; |
| 147 |
int width; |
| 148 |
int height; |
| 149 |
} app; |