void character::init() {
    if (!loaded) {
        loaded = load_frames();
    }
}
void redraw_window() {
    struct frame *frame;
    glLoadIdentity();
    glViewport(0,0, active_window->w, active_window->h);
	glOrtho(0, 1, 0, 1,0,1);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    switch (active_window->state) {
    case STATE_LOADING:
        glColor3f(1.0f, 1.0f, 1.0f);
        glutBitmapString(GLUT_BITMAP_HELVETICA_18, "Loading frames ... Please wait.");
        load_frames(active_window);
        active_window->state = STATE_VIEW;
        break;
    case STATE_VIEW:
        // Get frame
        frame = get_frame(active_window->frames, active_window->cur);
        if (frame == 0) {
            printf("Frame #%d wasn't found\n", active_window->cur);
            return;
        }

        // Draw image
        glBindTexture(GL_TEXTURE_2D, frame->texture);
        glEnable(GL_TEXTURE_2D);
        glBegin(GL_QUADS);
            glTexCoord2f(0.0,1.0);
            glVertex2f(0,0);
            glTexCoord2f(1.0,1.0);
            glVertex2i(1.0f, 0.0f);
            glTexCoord2f(1.0f,0.0f);
            glVertex2f(1.0f, 1.0f);
            glTexCoord2f(0.0f, 0.0f);
            glVertex2f(0,1.0f);
        glEnd();
        glDisable(GL_TEXTURE_2D);

        // Draw text
        glColor3f(1.0f, 1.0f, 1.0f);
        char *fr_str = malloc(255);
        char *rad_str = malloc(255);
        sprintf(fr_str, "Frame %d of %d", active_window->cur, active_window->nfr);
        sprintf(rad_str,"Radius: %d", active_window->radius);

        glRasterPos2f(0.0f, 0.0f);
        glutBitmapString(GLUT_BITMAP_HELVETICA_12, fr_str);
        glutBitmapString(GLUT_BITMAP_HELVETICA_12, rad_str);

        // If we have hough transformed it, draw a circle
        if (frame->flag & HAS_HOUGH && frame->hough.found) {
            glLoadIdentity();
            glOrtho(0, active_window->w, active_window->h, 0, 0, 1);
	        float theta;
            float x0, y0, r;
            x0 = frame->hough.x;
            y0 = frame->hough.y;
            r = frame->hough.radius;


            glBegin(GL_LINE_LOOP);
                // Draw circle
                glColor3f(1.0f, 0.0f, 0.0f);
                for (theta = 0.0f; theta < 2*M_PI; theta += 0.01f) {
                    glVertex2f(x0+r*cos(theta), y0+r*sin(theta));
                }

                glColor3f(1.0f, 1.0f, 1.0f);
            glEnd();
        }

        // Draw square around match
        if (frame->flag & HAS_MATCH) {
            glLoadIdentity();
            glOrtho(0, active_window->w, active_window->h, 0, 0, 1);
            float w = (float)active_window->tmpl->cols, h = (float)active_window->tmpl->rows;

            glBegin(GL_LINE_LOOP);
                glColor3f(1.0f, 0.0f, 0.0f);
                glVertex2f(frame->match.x+w, frame->match.y+h);
                glVertex2f(frame->match.x+w, frame->match.y);
                glVertex2f(frame->match.x, frame->match.y);
                glVertex2f(frame->match.x, frame->match.y+h);
                glColor3f(1.0f, 1.0f, 1.0f);
            glEnd();
        }

        free(fr_str);
        free(rad_str);
        break;
    }

	glFlush();
}