teaching machines

CS 330 Lecture 18 – Callbacks and XML

March 7, 2012 by . Filed under cs330, lectures, spring 2012.

Agenda

TODO

Code

torus.c

#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>

/* ------------------------------------------------------------------------- */

int x_angle = 0;
int y_angle = 0;

/* ------------------------------------------------------------------------- */

void draw_callback() {
  glClear(GL_COLOR_BUFFER_BIT);
  glLoadIdentity();
  glRotatef(x_angle, 1.0f, 0.0f, 0.0f);
  glRotatef(y_angle, 0.0f, 1.0f, 0.0f);
  glutWireTorus(0.3f, 0.5f, 10, 10);
  glutSwapBuffers();
}

/* ------------------------------------------------------------------------- */

void key_callback(unsigned char key,
                  int mouse_x,
                  int mouse_y) {
  switch (key) {
    case 'x':
      x_angle += 1;
      break;
    case 'y':
      y_angle += 1;
      break;
    case 'X':
      x_angle -= 1;
      break;
    case 'Y':
      y_angle -= 1;
      break;
  }

  glutPostRedisplay();
}

/* ------------------------------------------------------------------------- */

int main(int argc, char **argv) {
  glutInit(&argc, argv);
  glutCreateWindow("My first torus");
  glutDisplayFunc(draw_callback);
  glutKeyboardFunc(key_callback);
  glutMainLoop();

  return 0;
}

test.xml

<?xml version="1.0" encoding="UTF-8"?>
<root>
 <child color="purple">foo</child>
</root>

parse_xml.c

#include <expat.h>
#include <stdio.h>
#include <stdlib.h>

/* typedef parser_t * XML_Parser; */

/* ------------------------------------------------------------------------- */

void start(void *data,
           const char *tag,
           const char **attributes) {
  printf("tag: %s\n", tag);
}

/* ------------------------------------------------------------------------- */

void end(void *data,
         const char *tag) {
  printf("tag: %s\n", tag);
}

/* ------------------------------------------------------------------------- */

int main(int argc, char **argv) {

  XML_Parser parser = XML_ParserCreate(NULL);
  XML_SetElementHandler(parser, start, end);
  XML_ParserFree(parser);

  return 0;
}

Haiku

Fight over symbols.
Then the rest of us can work.
Steady wins races.