static void *_loader(void *unused) { FILE *f; char *buffer; size_t length; struct resource *r, *active_resources = NULL, *free_resources = NULL, *r_next; struct loaderMessage *m; unsigned int sequence = 0; while(1) { delt_with: m = _dequeue(); if(m == NULL) { nos_sleep(16); continue; } if(m->tag == MSG_EXIT) { r = active_resources; while(r) { r_next = r->next; r->release(r->data); #ifdef DEBUG free(r->filename); #endif r->next = free_resources; free_resources = r; r = r_next; } #ifdef DEBUG r = free_resources; while(r) { r_next = r->next; free(r); r = r_next; } #endif return NULL; } if(m->tag == MSG_BARRIER) { r = active_resources; active_resources = NULL; while(r) { r_next = r->next; if(r->sequence != sequence) { r->release(r->data); free(r->filename); r->next = free_resources; free_resources = r; } else { r->next = active_resources; active_resources = r; } r = r_next; } sequence++; continue; } r = active_resources; while(r) { if(strcmp(r->filename, m->filename) == 0) { r->sequence = sequence; *m->ptr = r->data; free(m->filename); goto delt_with; } r = r->next; } if(free_resources) { r = free_resources; free_resources = r->next; } else { r = calloc(1, sizeof(*r)); } f = fopen(m->filename, "r"); if(f == NULL) { continue; } fseek(f, 0, SEEK_END); length = ftell(f); fseek(f, 0, SEEK_SET); buffer = malloc(length + 1); if(buffer == NULL) { fclose(f); continue; } fread(buffer, 1, length, f); fclose(f); buffer[length] = 0; r->data = m->load(length, buffer); free(buffer); if(r->data) { *m->ptr = r->data; r->filename = m->filename; r->release = m->release; r->sequence = sequence; r->next = active_resources; active_resources = r; } } }
int nos_frame(void) { unsigned int q, v; struct nos_model *mod = &model; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if(camera_rotate) { int x, y; nos_mouse_delta(&x, &y); camera_angles[0] += y * 0.5f; camera_angles[1] += x * 0.5f; } camera_distance_actual = camera_distance_actual*0.9f + camera_distance*0.1f; glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0f, 0.0f, -camera_distance_actual); glRotatef(camera_angles[0], 1.0f, 0.0f, 0.0f); // up/down glRotatef(camera_angles[1], 0.0f, 1.0f, 0.0f); // left/right glRotatef(camera_angles[2], 0.0f, 0.0f, 1.0f); if(moving_code[0]) { int x; nos_mouse_delta(&x, NULL); mod = &moving_model; if(x) { moving_delta += x * 0.01f; if(moving_delta > 3.99f) { moving_delta = 3.99f; } if(moving_delta < -3.99f) { moving_delta = -3.99f; } moving_model = model; *((unsigned short *)&moving_code[1]) = (unsigned short)((moving_delta + 4.0f) * 8192.0f); nos_model_command(moving_code, &unpacker, mod); } } else { select_quad(); } glBegin(GL_QUADS); for(q = 0; q < mod->quads; ++q) { if(unpacker.deleted_quads[q]) { continue; } if(unpacker.selected_quads[q]) { if(q == best_quad) { glColor3f(0.6f, 0.6f, 0.3f); } else { glColor3f(0.5f, 0.5f, 0.2f); } } else { if(q == best_quad) { glColor3f(0.2f, 0.2f, 0.6f); } else { glColor3f(0.1f, 0.1f, 0.5f); } } glVertex3fv(mod->vertex_pos[mod->quad_index[q][0]]); glVertex3fv(mod->vertex_pos[mod->quad_index[q][1]]); glVertex3fv(mod->vertex_pos[mod->quad_index[q][2]]); glVertex3fv(mod->vertex_pos[mod->quad_index[q][3]]); } glEnd(); if(show_axis_lines) { glBegin(GL_LINES); glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.0f); glVertex3f(10.0f, 0.0f, 0.0f); glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.0f); glVertex3f(0.0f, 10.0f, 0.0f); glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(0.0f, 0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 10.0f); glEnd(); } if(show_edges) { glColor3f(0.2f, 0.2f, 1.0f); glBegin(GL_LINES); for(q = 0; q < mod->quads; ++q) { glVertex3fv(mod->vertex_pos[mod->quad_index[q][0]]); glVertex3fv(mod->vertex_pos[mod->quad_index[q][1]]); glVertex3fv(mod->vertex_pos[mod->quad_index[q][1]]); glVertex3fv(mod->vertex_pos[mod->quad_index[q][2]]); glVertex3fv(mod->vertex_pos[mod->quad_index[q][2]]); glVertex3fv(mod->vertex_pos[mod->quad_index[q][3]]); glVertex3fv(mod->vertex_pos[mod->quad_index[q][3]]); glVertex3fv(mod->vertex_pos[mod->quad_index[q][0]]); } glEnd(); } if(show_quad_normals) { glColor3f(0.25f, 0.25f, 0.25f); glBegin(GL_LINES); for(q = 0; q < mod->quads; ++q) { glVertex3fv(unpacker.quad_center[q]); glVertex3f(unpacker.quad_center[q][0] + mod->quad_normal[q][0], unpacker.quad_center[q][1] + mod->quad_normal[q][1], unpacker.quad_center[q][2] + mod->quad_normal[q][2]); } glEnd(); } if(show_vertex_normals) { glColor3f(0.25f, 0.25f, 0.25f); glBegin(GL_LINES); for(v = 0; v < mod->vertexes; ++v) { glVertex3fv(mod->vertex_pos[v]); glVertex3f(mod->vertex_pos[v][0] + mod->vertex_normal[v][0], mod->vertex_pos[v][1] + mod->vertex_normal[v][1], mod->vertex_pos[v][2] + mod->vertex_normal[v][2]); } glEnd(); } glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glScalef(2.0f, -2.0f, 0.0f); glTranslatef(-0.5f, -0.5f, 0.0f); glScalef(1.0f/NOS_XRES, 1.0f/NOS_YRES, 0.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glDisable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); glColor3f(0.2f, 0.2f, 0.2f); glRectf(0.0f, 0.0f, 128.0f, NOS_YRES); glColor3f(1.0f, 1.0f, 1.0f); { float y = 0; for(q = 0; q < command_index; q++) { nos_draw_text(2.0f, y += 12, cmd2text[command[q].code[0]], command[q].code[1] | command[q].code[2] << 8); } } { float y = 0; switch(last_sym) { case 't': nos_draw_text(128.0f, y += 12, "Translate faces"); nos_draw_text(128.0f, y += 12, "n: normal"); nos_draw_text(128.0f, y += 12, "x: X"); nos_draw_text(128.0f, y += 12, "y: Y"); nos_draw_text(128.0f, y += 12, "z: Z"); break; case 'T': nos_draw_text(128.0f, y += 12, "Translate all"); nos_draw_text(128.0f, y += 12, "x: X"); nos_draw_text(128.0f, y += 12, "y: Y"); nos_draw_text(128.0f, y += 12, "z: Z"); break; case 's': nos_draw_text(128.0f, y += 12, "Scale faces"); nos_draw_text(128.0f, y += 12, "n: normal"); nos_draw_text(128.0f, y += 12, "x: X"); nos_draw_text(128.0f, y += 12, "y: Y"); nos_draw_text(128.0f, y += 12, "z: Z"); break; case 'S': nos_draw_text(128.0f, y += 12, "Scale all"); nos_draw_text(128.0f, y += 12, "x: X"); nos_draw_text(128.0f, y += 12, "y: Y"); nos_draw_text(128.0f, y += 12, "z: Z"); break; case 'r': nos_draw_text(128.0f, y += 12, "Rotate faces"); nos_draw_text(128.0f, y += 12, "x: X"); nos_draw_text(128.0f, y += 12, "y: Y"); nos_draw_text(128.0f, y += 12, "z: Z"); break; case 'e': nos_draw_text(128.0f, y += 12, "Extra"); nos_draw_text(128.0f, y += 12, "s: Smooth all"); nos_draw_text(128.0f, y += 12, "d: Subdivide all"); nos_draw_text(128.0f, y += 12, "a: Add Cube"); nos_draw_text(128.0f, y += 12, "i: Inset faces"); nos_draw_text(128.0f, y += 12, "e: Extrude faces"); nos_draw_text(128.0f, y += 12, "t: Taper faces"); nos_draw_text(128.0f, y += 12, "r: Remove faces"); nos_draw_text(128.0f, y += 12, "c: Cage faces"); break; case 'x': nos_draw_text(128.0f, y += 12, "Select"); nos_draw_text(128.0f, y += 12, "a: All"); nos_draw_text(128.0f, y += 12, "n: None"); nos_draw_text(128.0f, y += 12, "i: Invert"); nos_draw_text(128.0f, y += 12, "g: Grow selection"); nos_draw_text(128.0f, y += 12, "j: Select Adjacent"); nos_draw_text(128.0f, y += 12, "x: Simular X"); nos_draw_text(128.0f, y += 12, "y: Simular Y"); nos_draw_text(128.0f, y += 12, "z: Simular Z"); break; default: nos_draw_text(128.0f, y += 12, "t: Translate Face"); nos_draw_text(128.0f, y += 12, "T: Translate All"); nos_draw_text(128.0f, y += 12, "s: Scale Face"); nos_draw_text(128.0f, y += 12, "S: Scale All"); nos_draw_text(128.0f, y += 12, "r: Rotate Faces"); nos_draw_text(128.0f, y += 12, "e: Extra"); break; } } glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); glMatrixMode(GL_PROJECTION); glPopMatrix(); nos_sleep(10); return 0; }
inline void nos_run_task(void) { while (_nos_run_next_task()) {}; nos_sleep(); _nos_wait(); }