From d7575bef1e530eac004c973b0384a12561f0bb8b Mon Sep 17 00:00:00 2001 From: pixel Date: Mon, 20 Oct 2003 16:21:24 +0000 Subject: Commit of the month --- src/test.cc | 576 ++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 527 insertions(+), 49 deletions(-) (limited to 'src') diff --git a/src/test.cc b/src/test.cc index ec00bfe..0ef8a57 100644 --- a/src/test.cc +++ b/src/test.cc @@ -1,6 +1,13 @@ #include +#define _USE_MATH_DEFINES #include #include +#include +extern "C" { +#include +#include +#include +} #ifdef HAVE_CONFIG_H #include "config.h" #else @@ -19,24 +26,495 @@ #include "glsprite.h" #include "glshape.h" +#define SPIN .50 +#define TILT .20 + +#define NBSTARS 10000 +#define STARSRATE 300.0 + +#define LUA + +int zoom = 1; + +#define centerx 320 +#define centery 240 + +struct tdvect { + double x, y, z; +}; + +std::vector points; +std::vector ppoints; + +static int myprint(lua_State * L) { + const char * t = lua_tostring(L, -1); + + Base::printm(M_INFO, "From LUA: %s\n", t); + + return 0; +} + +static int myluaputstar(lua_State * L) { + tdvect t; + + t.x = lua_tonumber(L, -3); + t.y = lua_tonumber(L, -2); + t.z = lua_tonumber(L, -1); + + points.push_back(t); + + return 0; +} + +int myluaerror(lua_State * L) { + int n = lua_gettop(L); + int i; + String t; + + Base::printm(M_ERROR, "Got an LUA error\n"); + Base::printm(M_ERROR, "Inspecting LUA stack\n"); + + if (n == 0) { + Base::printm(M_ERROR, "Stack empty\n"); + return 0; + } + + for (i = 1; i <= n; i++) { + switch(lua_type(L, i)) { + case LUA_TNONE: + t = "Invalid"; + break; + case LUA_TNIL: + t = "(Nil)"; + break; + case LUA_TNUMBER: + t.set("(Number) %f", lua_tonumber(L, i)); + break; + case LUA_TBOOLEAN: + t.set("(Bool) %s", lua_toboolean(L, i) ? "true" : "false"); + break; + case LUA_TSTRING: + t.set("(String) %s", lua_tostring(L, i)); + break; + case LUA_TTABLE: + t.set("(Table)"); + break; + case LUA_TFUNCTION: + t = "(Function)"; + break; + default: + t = "Unknown"; + } + + Base::printm(M_ERROR, String(i) + ": " + t + "\n"); + } + + return 0; + + getchar(); +} + +unsigned int swap32(unsigned int w) { + return (w >> 24) | ((w >> 8) & 0x0000ff00) | ((w << 8) & 0x00ff0000) | (w << 24); +} + +unsigned int readB32(FILE * f) { + unsigned int r; + fread(&r, 1, 4, f); + return swap32(r); +} + +unsigned short readB16(FILE * f) { + unsigned char t[2]; + fread(t, 1, 2, f); + return t[1] | (t[0] << 8); +} + +float tofloat(unsigned int w) { + return *((float *) &w); +} + +char * sread(FILE * f) { + char temp[256], * p = temp; + + do { + *p = fgetc(f); + } while (*(p++)); + + return strdup(temp); +} + +char * pname[16] = { + "Unknown (not a float)", + "ScalX", + "ScalY", + "ScalZ", + "Unknown (not a float)", + "Unknown (not a float)", + "TransX", + "TransY", + "TransZ", + "Unknown", + "Unknown", + "Unknown", + "Unknown", + "Unknown", + "Unknown", + "Unknown" +}; + +#define OUTPUT stdout + +unsigned short entries; +struct tdvect * mesh; + +int bdlload(char * name) { + FILE * bdl; + int i, j; + char sig[9], sec[5], ** bnames; + unsigned int length, fpos, namespos, tpos, npos, r; + unsigned short * unks; + float f; + + if (!(bdl = fopen(name, "rb"))) + exit(-1); + + fread(sig, 1, 8, bdl); + sig[8] = 0; + + if (strcmp(sig, "J3D2bdl4")) { + fprintf(OUTPUT, "File not bdl.\n"); + exit(-1); + } + + fprintf(OUTPUT, "Looking for section JNT1...\n"); + + fseek(bdl, 32, SEEK_SET); + + do { + fread(sec, 1, 4, bdl); + sec[4] = 0; + length = readB32(bdl); + fprintf(OUTPUT, "Found section `%s' @ %08x, size %08x\n", sec, ftell(bdl) - 8, length); + fseek(bdl, length - 8, SEEK_CUR); + } while (strcmp(sec, "JNT1")); + + fseek(bdl, -length, SEEK_CUR); + fpos = ftell(bdl); + + fprintf(OUTPUT, "Found it at 0x%08x\n", fpos); + + fseek(bdl, 8, SEEK_CUR); + + entries = readB16(bdl); + if (readB16(bdl) != 0xffff) { + fprintf(OUTPUT, "Something's wrong, not found FFFF\n"); + } + if (readB32(bdl) != 0x18) { + fprintf(OUTPUT, "Something's wrong, bones not at 0x18, can't go on.\n"); + exit(-1); + } + if (readB32(bdl) != (0x18 + entries * 0x40)) { + fprintf(OUTPUT, "Something's wrong, bone index not at expected place, can't go on.\n"); + exit(-1); + } + + namespos = readB32(bdl); + + fprintf(OUTPUT, "Reading names.\n"); + + bnames = (char **) malloc(entries * sizeof(char *)); + unks = (unsigned short *) malloc(entries * sizeof(unsigned short)); + mesh = (struct tdvect *) malloc(entries * sizeof(struct tdvect)); + + fseek(bdl, namespos + fpos + 4, SEEK_SET); + + for (i = 0; i < entries; i++) { + unks[i] = readB16(bdl); + npos = readB16(bdl) + fpos + namespos; + tpos = ftell(bdl); + fseek(bdl, npos, SEEK_SET); + bnames[i] = sread(bdl); + fseek(bdl, tpos, SEEK_SET); + } + for (i = 0; i < entries; i++) { + fprintf(OUTPUT, "%02x: (%04x) `%s'\n", i, unks[i], bnames[i]); + } + + fprintf(OUTPUT, "Dumping bones\n"); + + fseek(bdl, fpos + 0x18, SEEK_SET); + + for (i = 0; i < entries; i++) { + fprintf(OUTPUT, "Bone #%02x (%s):\n", i, bnames[i]); + for (j = 0; j < 16; j++) { + r = readB32(bdl); + f = tofloat(r); + fprintf(OUTPUT, "%x: %08x - %9.5f - %s\n", j, r, f, pname[j]); + switch(j) { + case 6: + mesh[i].x = f; + break; + case 7: + mesh[i].y = f; + break; + case 8: + mesh[i].z = f; + break; + } + } + } + + return 0; +} + + CODE_BEGINS + +void checkluastack(lua_State * L) { + int n = lua_gettop(L); + int i; + String t; + + printm(M_INFO, "Inspecting LUA stack\n"); + + if (n == 0) { + printm(M_INFO, "Stack empty\n"); + return; + } + + for (i = 1; i <= n; i++) { + switch(lua_type(L, i)) { + case LUA_TNONE: + t = "Invalid"; + break; + case LUA_TNIL: + t = "(Nil)"; + break; + case LUA_TNUMBER: + t.set("(Number) %f", lua_tonumber(L, i)); + break; + case LUA_TBOOLEAN: + t.set("(Bool) %s", lua_toboolean(L, i) ? "true" : "false"); + break; + case LUA_TSTRING: + t.set("(String) %s", lua_tostring(L, i)); + break; + case LUA_TTABLE: + t.set("(Table)"); + break; + case LUA_TFUNCTION: + t = "(Function)"; + break; + default: + t = "Unknown"; + } + + printm(M_INFO, String(i) + ": " + t + "\n"); + } +} + +void putstar(double x, double y, double z, mogltk::Sprite * s) { + double tx, ty, tz; + double spin, tilt; + + spin = (mogltk::engine::mouseX() - centerx) / 80.0; + tilt = (mogltk::engine::mouseY() - centery) / 80.0; + + // Spin --> around y + + tx = x * cos(spin) + z * sin(spin); + tz = -x * sin(spin) + z * cos(spin); + + x = tx; + z = tz; + + + // tilt --> around x + + ty = y * cos(tilt) + z * sin(tilt); + tz = -y * sin(tilt) + z * cos(tilt); + + y = ty; + z = tz; + + tdvect t; + t.x = x; + t.y = y; + t.z = z; + +// s->draw(centerx + t.x * zoom * mogltk::engine::mouseZ() - s->GetSX() / 2, centery + t.y * zoom * mogltk::engine::mouseZ() - s->GetSY() / 2); + ppoints.push_back(t); +} + +double t; + +#ifdef STARSCAPE +int starsx[NBSTARS]; +int starsy[NBSTARS]; +#endif + +#ifdef SPHERE +double starst[NBSTARS]; +double starso[NBSTARS]; +#endif + +lua_State * L; + +void initstars() { + int i; +#ifdef STARSCAPE + for (i = 0; i < NBSTARS; i++) { + starsx[i] = rand() % 640; + starsy[i] = rand() % 480; + } + + t = 0; +#endif +#ifdef RING + t = 0; +#endif +#ifdef SPHERE + t = 0; + for (i = 0; i < NBSTARS; i++) { + starst[i] = -(rand() % 3000) / 1000.0; + starso[i] = 2 * ((rand() % 1000) / 1000.0) * M_PI; + } +#endif +#ifdef LUA + L = lua_open(); + luaopen_math(L); + lua_atpanic(L, myluaerror); + luaL_loadfile(L, "particules.lua"); + lua_call(L, 0, 0); + lua_pushstring(L, "putstar"); + lua_pushcfunction(L, myluaputstar); + lua_settable(L, LUA_GLOBALSINDEX); + lua_pushstring(L, "print"); + lua_pushcfunction(L, myprint); + lua_settable(L, LUA_GLOBALSINDEX); + lua_pushstring(L, "initstars"); + lua_gettable(L, LUA_GLOBALSINDEX); + lua_call(L, 0, 0); +#endif +} + +void displaystars(mogltk::Sprite * s) { + int i; + double x, y, z, st; + tdvect tv; + +#ifdef STARSCAPE + for (i = 0; i < NBSTARS; i++) { + s->draw(starsx[i], starsy[i]); + } +#endif +#ifdef RING + for (i = 0; i < NBSTARS; i++) { + putstar(cos(t + i * 2 * M_PI / NBSTARS), 0, sin(t + i * 2 * M_PI / NBSTARS), s); + } +#endif +#ifdef SPHERE + for (i = 0; i < NBSTARS; i++) { + st = t - starst[i]; + + y = st / 3.0; + + x = cos(st * 2 * M_PI + starso[i]) * sin(y * M_PI); + z = sin(st * 2 * M_PI + starso[i]) * sin(y * M_PI); + + y = cos(y * M_PI); + + putstar(x, y, z, s); + } +#endif +#ifdef LUA + lua_pushstring(L, "displaystars"); + lua_gettable(L, LUA_GLOBALSINDEX); + lua_call(L, 0, 0); + + for (std::vector::iterator i = points.begin(); i != points.end(); i++) { + tv = *i; + putstar(tv.x, tv.y, tv.z, s); + } + points.clear(); +#endif +#ifdef BDL + for (i = 0; i < entries; i++) { + putstar(mesh[i].x / 20, mesh[i].y / 20, mesh[i].z / 20, s); + } +#endif + while (!ppoints.empty()) { + std::vector::iterator m = ppoints.begin(); + for (std::vector::iterator i = ppoints.begin(); i != ppoints.end(); i++) { + if (i->z > m->z) + m = i; + } + tv = *m; + ppoints.erase(m); + // Only bare projecting... for now... + s->draw(centerx + tv.x * zoom * mogltk::engine::mouseZ() - s->GetSX() / 2, centery + tv.y * zoom * mogltk::engine::mouseZ() - s->GetSY() / 2); + } +} + +void incrementstars(double nt) { + int i; + +#ifdef STARSCAPE + while ((t + (1 / STARSRATE)) <= nt) { + t += 1 / STARSRATE; + + for (i = 0; i < NBSTARS; i++) { + if ((++starsx[i]) >= 640) { + starsx[i] = 0; + starsy[i] = rand() % 480; + } + } + } +#endif +#ifdef RING + t = nt; +#endif +#ifdef SPHERE + t = nt; + + for (i = 0; i < NBSTARS; i++) { + if ((starst[i] + 3) < t) { + starst[i] += 3.0; + starso[i] = 2 * ((rand() % 1000) / 1000.0) * M_PI; + } + } +#endif +#ifdef LUA + t = nt; + + lua_pushstring(L, "incstars"); + lua_gettable(L, LUA_GLOBALSINDEX); + lua_pushnumber(L, t); + lua_call(L, 1, 0); +#endif +} + virtual int startup() throw (GeneralException) { int sx1, sx2, sy1, sy2; double t = 0; verbosity = M_INFO; new Archive("datas.paq"); + bdlload("cl.bdl"); #if 1 - mogltk::glbase * gl = new mogltk::glbase(); - mogltk::glshape * sh = new mogltk::glshape(); - mogltk::glfont * font = new mogltk::glfont(&Input("font-2.bin")); - mogltk::glSprite * s = new mogltk::glSprite(&Input("cursor.rgba"), 25, 25); + mogltk::base * gl = new mogltk::glbase(); + mogltk::shape * sh = new mogltk::glshape(); + mogltk::font * font = new mogltk::glfont(&Input("font-2.bin")); + mogltk::Sprite * s = new mogltk::glSprite(&Input("cursor.rgba"), 25, 25); + mogltk::Sprite * p = new mogltk::glSprite(&Input("particule.rgba"), 16, 16); #else mogltk::base * gl = new mogltk::base(); mogltk::shape * sh = new mogltk::shape(); mogltk::font * font = new mogltk::font(&Input("font-2.bin")); mogltk::Sprite * s = new mogltk::Sprite(&Input("cursor.rgba"), 25, 25); + mogltk::Sprite * p = new mogltk::Sprite(&Input("particule.rgba"), 16, 16); #endif mogltk::engine::setcursorvisible(true); @@ -47,69 +525,69 @@ virtual int startup() throw (GeneralException) { Color AlphaBlue(AQUA); AlphaBlue.A = 50; - mogltk::fill * f = sh->fcircle(320, 240, 50); +// mogltk::fill * f = sh->fcircle(320, 240, 50); + + initstars(); while (!mogltk::engine::quitrequested()) { - sx1 = 320 + 320 * sin(0.983 * t + 3.15); - sx2 = 320 + 320 * sin(0.537 * t + 5.32); - sy1 = 240 + 240 * sin(0.692 * t + 8.21); - sy2 = 240 + 240 * sin(1.029 * t + 2.42); +// sx1 = 320 + 320 * sin(0.983 * t + 3.15); +// sx2 = 320 + 320 * sin(0.537 * t + 5.32); +// sy1 = 240 + 240 * sin(0.692 * t + 8.21); +// sy2 = 240 + 240 * sin(1.029 * t + 2.42); gl->Enter2DMode(); sh->tbox(mytex, 50, 50, 561, 561, BLACK, RED, LIME, BLUE); - sh->box(400, 100, 450, 150, BLACK, RED, LIME, BLUE); - - sh->box(5, 5, 150, 80, CORNFLOWERBLUE, DEEPSKYBLUE, MIDNIGHTBLUE, NAVY); - font->setshadow(1); - font->putcursor(10, 30); - font->setcolor(WHITE); - font->printf( - "PixelPawa!\n" - "It works!!\n" - "I can't believe it!\n" - ); +// sh->box(400, 100, 450, 150, BLACK, RED, LIME, BLUE); + +// sh->box(5, 5, 150, 80, CORNFLOWERBLUE, DEEPSKYBLUE, MIDNIGHTBLUE, NAVY); + mogltk::ColorP::Max.A = 100; + sh->box(5, 5, 400, 300, CORNFLOWERBLUE, DEEPSKYBLUE, MIDNIGHTBLUE, NAVY); + mogltk::ColorP::Max.A = 255; +// font->setshadow(1); +// font->putcursor(10, 30); +// font->setcolor(WHITE); +// font->printf( +// "PixelPawa!\n" +// "It works!!\n" +// "I can't believe it!\n" +// ); - sh->box3d(50, 150, 150, 200); - sh->obox3d(50, 250, 150, 300); - sh->window(50, 350, 150, 400, "Titre plus beau ;)"); - sh->box3d(180, 130, 320, 220); - sh->button(200, 150, 300, 200, "Bouton"); - - sh->fdraw(f, BLUE); - sh->sdraw(f); +// sh->box3d(50, 150, 150, 200); +// sh->obox3d(50, 250, 150, 300); +// sh->window(50, 350, 150, 400, "Titre plus beau ;)"); +// sh->box3d(180, 130, 320, 220); +// sh->button(200, 150, 300, 200, "Bouton"); + +// sh->fdraw(f, BLUE); +// sh->sdraw(f); - sh->box(MIN(sx1, sx2), MIN(sy1, sy2), MAX(sx1, sx2), MAX(sy1, sy2), AlphaBlue); - mogltk::ColorP::Min.A = 200; - sh->obox(MIN(sx1, sx2), MIN(sy1, sy2), MAX(sx1, sx2), MAX(sy1, sy2), AlphaBlue); - mogltk::ColorP::Min.A = 0; +// sh->box(MIN(sx1, sx2), MIN(sy1, sy2), MAX(sx1, sx2), MAX(sy1, sy2), AlphaBlue); +// mogltk::ColorP::Min.A = 200; +// sh->obox(MIN(sx1, sx2), MIN(sy1, sy2), MAX(sx1, sx2), MAX(sy1, sy2), AlphaBlue); +// mogltk::ColorP::Min.A = 0; font->putcursor(550, 400); font->printf("FPS: %.2f\n", mogltk::engine::FPS()); font->printf("mx: %i\n", mogltk::engine::mouseX()); font->printf("my: %i\n", mogltk::engine::mouseY()); - font->printf("t: %.2fs\n", (double) SDL_GetTicks() / 1000); - - double sx, sy, l, a; - sx = mogltk::engine::mouseX() - 320; - sy = mogltk::engine::mouseY() - 240; - l = sqrt(sx * sx + sy * sy); - sx /= l; - sy /= l; - a = acos(sx); - if (sy < 0) - a = -a; - - font->printf("a: %.2f\n", a); - sh->line(320, 240, mogltk::engine::mouseX(), mogltk::engine::mouseY()); - s->drawrotate(320, 240, a); + font->printf("t: %.2fs\n", SDL_GetTicks() / 1000.0); + + p->draw(mogltk::engine::mouseX() - 8, mogltk::engine::mouseY() - 8); s->draw(mogltk::engine::mouseX() - 8, mogltk::engine::mouseY() - 6); +// sh->line(320, 240, mogltk::engine::mouseX(), mogltk::engine::mouseY()); - gl->Leave2DMode(); + mogltk::ColorP::Min.A = 200; +// p->draw(sx1, sy1, AlphaBlue); +// p->draw(sx2, sy2, AlphaBlue); + displaystars(p); + incrementstars(t); + + gl->Leave2DMode(); gl->Flip(); - t = (double) SDL_GetTicks() / 1000; + t = (double) SDL_GetTicks() / 1000.0; } delete mytex; -- cgit v1.2.3