Create a gist now

Instantly share code, notes, and snippets.

Bitmap creator for ST7565 based displays
//#include<windows.h>
#define GLUT_DISABLE_ATEXIT_HACK
#include <GL/glut.h>
#include <stdio.h>
#include <string.h>
#define background 1, 1, 1
#define foreground 0, 0, 0
unsigned int width = 5;
unsigned int height = 8;
unsigned int byteCount = (height/8);
int cx, cy;//click mouse pos
int gw,gh = 0;//window width and height
uint8_t *matrix = NULL;
void init( void ){
glClearColor(background, 0.0);
}
void reshape(int w, int h){
gw=w;gh=h;
if (w == 0) w = 1;
if (h == 0) h = 1;
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.,1.,0.,1.);
glutPostRedisplay();
glutInitWindowSize(320,320);
}
void drawBox(float ax, float ay, float bx, float by, float cx, float cy, float dx, float dy, float r, float g, float b)
{
glBegin(GL_LINE_STRIP);
glColor3f(r,g,b); /*kolor rysowania D_____ C */
glVertex2f(ax,ay); /*A(x,y) | | */
glVertex2f(bx,by); /*B(x,y) | | */
glVertex2f(cx,cy); /*C(x,y) A|_____|B */
glVertex2f(dx,dy);
glVertex2f(ax,ay);
glEnd();
}
void drawRect(float ax, float ay, float bx, float by, float cx, float cy, float dx, float dy, float r, float g, float b)
{
glBegin(GL_QUADS);
glColor3f(r,g,b); /*kolor rysowania D_____ C */
glVertex2f(ax,ay); /*A(x,y) | | */
glVertex2f(bx,by); /*B(x,y) | | */
glVertex2f(cx,cy); /*C(x,y) A|_____|B */
glVertex2f(dx,dy);
glVertex2f(ax,ay);
glEnd();
}
void display(void){
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
float i,j;
int k,l;
int m;
for(m = 0; m < byteCount; m++)
{
for(i = 0,k = 0; i < 1.; i+=1./width) /* X axis */
{
for(j = (1./byteCount)*m, l = 0; j < (1./byteCount)*(m+1); j+=1./height)//end on 1, 0.5, 0.25, ...
{
drawBox(
i, j,
i+1./width, j,
i+1./width, j+1./height,
i, j+1./height,
foreground
);
if((matrix[k+((byteCount-m-1)*width)] & (1<<(height/byteCount-1-l))) != 0 )//divide by num of bytes per column
drawRect(
i, j,
i+1./width, j,
i+1./width, j+1./height,
i, j+1./height,
foreground
);
l++;
}
k++;
}
}
glFinish();
}
void mouseFunc(int btn, int state, int x, int y)
{
int i;
switch(btn)
{
case GLUT_LEFT_BUTTON:
if(state==GLUT_UP)
{
//released
int mx,my, mcx,mcy = 0;
//release
mx = x/(gw/width);if(mx>width)mx=-1;
my = y/(gh/height);if(my>height)my=-1;
//click
mcx = cx/(gw/width);if(mcx>(width-1))mcx=-1;
mcy = cy/(gh/height);if(mcy>(height-1))mcy=-1;
for(i = 0; i < byteCount; i++)
{
if(mx==mcx && my==mcy && mx>=0 && mcx>=0 && my>=0 && mcy>=0 && my<((i+1)*8))
{
matrix[mx+(width*i)]^=1<<my-(8*i);
}
}
glutPostRedisplay();
}
else if(state==GLUT_DOWN)
{
//clicked
cx=x;
cy=y;
}
break;
}
}
void keyFunc(unsigned char key, int x, int y)
{
int i,j;
switch(key)
{
case 'r':
memset(matrix,'\0',width*byteCount);
glutPostRedisplay();
break;
case 'p':
for(j = 0; j < byteCount; j++)
{
for(i = width*j; i < width*(j+1); i++)
printf("0x%02x ",matrix[i]);
printf("\n");
}
break;
}
}
int main( int argc, char** argv )
{
if(argc>2)
{
width = atoi(argv[1]);
height = atoi(argv[2]);
byteCount = (height/8);
if(height % 8 != 0)
{
fprintf(stderr,"Error: tried to use height that is not divisible by 8\n");
return 1;
}
}
matrix = (uint8_t*) malloc(width*byteCount);
memset(matrix, 0x00, width*byteCount);
int mode = GLUT_RGB | GLUT_SINGLE;
glutInit( &argc, argv);
glutInitDisplayMode(mode);
glutInitWindowSize(320,448);
glutInitWindowPosition(100,100);
glutCreateWindow("glFontMaker");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouseFunc);
glutKeyboardFunc(keyFunc);
glutMainLoop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment