Skip to content

Instantly share code, notes, and snippets.

@tai2
Created April 3, 2013 23:35
Show Gist options
  • Save tai2/5306452 to your computer and use it in GitHub Desktop.
Save tai2/5306452 to your computer and use it in GitHub Desktop.
Improved version of https://gist.github.com/tai2/5008183 using glDrowPixels().
#include <stdio.h>
#include <stdlib.h>
#include <GLUT/glut.h>
#define KEY_ESC 27
#define WINDOW_POS_X 100
#define WINDOW_POS_Y 100
#define DEF_SCREEN_WIDTH 640
#define DEF_SCREEN_HEIGHT 360
static int curr_width = 0;
static int curr_height = 0;
static GLubyte *screen_buf = NULL;
static int fullscreen = 0;
static void
keyboard(unsigned char c, int x, int y)
{
switch (c) {
case KEY_ESC:
exit(0);
break;
case 'f':
if (fullscreen) {
glutReshapeWindow(DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT);
glutPositionWindow(WINDOW_POS_X, WINDOW_POS_Y);
fullscreen = 0;
} else {
glutFullScreen();
fullscreen = 1;
}
break;
}
}
void
idle()
{
glutPostRedisplay();
}
static void
reshape(int w, int h)
{
curr_width = w;
curr_height = h;
if (screen_buf) {
free(screen_buf);
}
screen_buf = (GLubyte *)malloc(curr_width * curr_height * 4);
if (!screen_buf) {
abort();
}
}
static void
draw_pixel(int x, int y, GLubyte r, GLubyte g, GLubyte b)
{
int base = 4 * ((curr_height - 1 - y) * curr_width + x);
screen_buf[base + 0] = r;
screen_buf[base + 1] = g;
screen_buf[base + 2] = b;
screen_buf[base + 3] = 255;
}
static void
display()
{
int x, y;
glClear(GL_COLOR_BUFFER_BIT);
for (y = 0; y < curr_height; y++) {
for (x = 0; x < curr_width; x++) {
GLubyte v = rand() % 256;
draw_pixel(x, y, v, v, v);
}
}
glWindowPos2i(0, 0);
glDrawPixels(curr_width, curr_height, GL_RGBA, GL_UNSIGNED_BYTE, screen_buf);
glutSwapBuffers();
}
int
main(int argc, char** argv)
{
srand(0);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutInitWindowSize(DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT);
glutInitWindowPosition(WINDOW_POS_X, WINDOW_POS_Y);
glutCreateWindow(argv[0]);
glutKeyboardFunc(keyboard);
glutIdleFunc(idle);
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glutMainLoop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment