From 537d99f7362abc5b8e07ca8ad2567f4c1b98bb2c Mon Sep 17 00:00:00 2001 From: Chip Black Date: Thu, 19 May 2011 01:17:31 -0500 Subject: [PATCH 1/1] Initial commit: basic port of SwarmDS. --- buildit.cmd | 42 +++++++ package/appinfo.json | 10 ++ package/framework_config.json | 7 ++ package/package.properties | 1 + packageit.cmd | 7 ++ src/Pixel.cpp | 45 ++++++++ src/Pixel.h | 18 +++ src/swarm.cpp | 196 +++++++++++++++++++++++++++++++ swarm.sln | 20 ++++ swarm.vcproj | 210 ++++++++++++++++++++++++++++++++++ 10 files changed, 556 insertions(+) create mode 100644 buildit.cmd create mode 100644 package/appinfo.json create mode 100644 package/framework_config.json create mode 100644 package/package.properties create mode 100644 packageit.cmd create mode 100644 src/Pixel.cpp create mode 100644 src/Pixel.h create mode 100644 src/swarm.cpp create mode 100644 swarm.sln create mode 100644 swarm.vcproj diff --git a/buildit.cmd b/buildit.cmd new file mode 100644 index 0000000..d88cc18 --- /dev/null +++ b/buildit.cmd @@ -0,0 +1,42 @@ +@echo off +@rem Set the device you want to build for to 1 +@rem Use Pixi to allow running on either device +set PRE=0 +set PIXI=1 +set DEBUG=1 + +@rem List your source files here +set SRC=src\swarm.cpp src\Pixel.cpp + +@rem List the libraries needed +set LIBS=-lSDL -lpdl -lGLES_CM + +@rem Name your output executable +set OUTFILE=swarm + +if %PRE% equ 0 if %PIXI% equ 0 goto :END + +if %DEBUG% equ 1 ( + set DEVICEOPTS=-g +) else ( + set DEVICEOPTS= +) + +if %PRE% equ 1 ( + set DEVICEOPTS=%DEVICEOPTS% -mcpu=cortex-a8 -mfpu=neon -mfloat-abi=softfp +) + +if %PIXI% equ 1 ( + set DEVICEOPTS=%DEVICEOPTS% -mcpu=arm1136jf-s -mfpu=vfp -mfloat-abi=softfp +) + +echo %DEVICEOPTS% + +arm-none-linux-gnueabi-gcc %DEVICEOPTS% -o %OUTFILE% %SRC% "-I%PALMPDK%\include" "-I%PALMPDK%\include\SDL" "-L%PALMPDK%\device\lib" -Wl,--allow-shlib-undefined %LIBS% + +goto :EOF + +:END +echo Please select the target device by editing the PRE/PIXI variable in this file. +exit /b 1 + diff --git a/package/appinfo.json b/package/appinfo.json new file mode 100644 index 0000000..60aad84 --- /dev/null +++ b/package/appinfo.json @@ -0,0 +1,10 @@ +{ + "id": "com.dominionofawesome.swarm", + "version": "0.9.0", + "vendor": "The Dominion of Awesome", + "type": "pdk", + "main": "swarm", + "title": "Swarm", + "icon": "icon.png", + "requiredMemory": 10 +} diff --git a/package/framework_config.json b/package/framework_config.json new file mode 100644 index 0000000..40d4e8d --- /dev/null +++ b/package/framework_config.json @@ -0,0 +1,7 @@ +{ + "logLevel": 99, + "debuggingEnabled": true, + "timingEnabled": false, + "logEvents": false, + "escapeHTMLInTemplates" : true +} diff --git a/package/package.properties b/package/package.properties new file mode 100644 index 0000000..ea83be1 --- /dev/null +++ b/package/package.properties @@ -0,0 +1 @@ +filemode.755 = swarm diff --git a/packageit.cmd b/packageit.cmd new file mode 100644 index 0000000..8d9967d --- /dev/null +++ b/packageit.cmd @@ -0,0 +1,7 @@ +@echo off +call buildit.cmd + +copy swarm package\ +arm-none-linux-gnueabi-strip package\swarm + +palm-package package diff --git a/src/Pixel.cpp b/src/Pixel.cpp new file mode 100644 index 0000000..5a27aee --- /dev/null +++ b/src/Pixel.cpp @@ -0,0 +1,45 @@ +#include "Pixel.h" +//#include +#include +#include "SDL.h" + +Pixel::Pixel(void) { + this->x = (rand() / (float)RAND_MAX) * 320.0; + this->y = (rand() / (float)RAND_MAX) * 400.0; + this->vx = (rand() / (float)RAND_MAX) * 10.0 - 5.0; + this->vy = (rand() / (float)RAND_MAX) * 10.0 - 5.0; + this->lastupdate = SDL_GetTicks(); +} + +Pixel::~Pixel(void) { +} + +void Pixel::accel(float x, float y) { + this->vx += x; + if (this->vx > 100.0) + this->vx = 100.0; + else if (this->vx < -100.0) + this->vx = -100.0; + this->vy += y; + if (this->vy > 100.0) + this->vy = 100.0; + else if (this->vy < -100.0) + this->vy = -100.0; +} + +void Pixel::update() { + Uint32 now = SDL_GetTicks(); + float dt = (now - this->lastupdate) / 75.0; + if (dt < 1000) { // If we're running less than 1FPS, don't bother updating. + this->x += this->vx * dt; + this->y += this->vy * dt; + } + this->lastupdate = now; +} + +void Pixel::draw(GLfloat *vertices) { + vertices[0] = this->x; + vertices[1] = this->y; + vertices[3] = this->x - this->vx / 4.0f; + vertices[4] = this->y - this->vy / 4.0f; +} \ No newline at end of file diff --git a/src/Pixel.h b/src/Pixel.h new file mode 100644 index 0000000..932b009 --- /dev/null +++ b/src/Pixel.h @@ -0,0 +1,18 @@ +#pragma once +#include +#include "SDL.h" + +class Pixel +{ +private: + Uint32 lastupdate; +public: + float x, y; + float vx, vy; + + Pixel(void); + ~Pixel(void); + void accel(float x, float y); + void update(); + void draw(GLfloat *); +}; diff --git a/src/swarm.cpp b/src/swarm.cpp new file mode 100644 index 0000000..78d5133 --- /dev/null +++ b/src/swarm.cpp @@ -0,0 +1,196 @@ +#include +#include + +#include +#include "SDL.h" +#include "PDL.h" +#include "Pixel.h" +#define NPIXELS 2000 + +SDL_Surface* screen; // Screen surface +Uint16 mx, my; +Pixel** pixels; +GLfloat* vertices; + +#ifdef WIN32 +extern "C" +#endif +GL_API int GL_APIENTRY _dgles_load_library(void *, void *(*)(void *, const char *)); + +static void *proc_loader(void *h, const char *name) +{ + (void) h; + return SDL_GL_GetProcAddress(name); +} + +void init() { + // Set up OpenGL + glClearColor(0.5f, 0.5f, 0.5f, 0.0f); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrthof(0.0f, 320.0f, 400.0f, 0.0f, -1.0f, 1.0f); + glColor4f(0.0f, 0.0f, 0.0f, 1.0f); + + // Initialize vertices + vertices = new GLfloat[NPIXELS * 6]; + memset(vertices, 0, sizeof(GLfloat) * NPIXELS * 6); + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(3, GL_FLOAT, 0, vertices); + + // Center cursor + mx = 160; + my = 200; + + // Initialize pixel objects + pixels = new Pixel*[NPIXELS]; + for (int x = 0; x < NPIXELS; x++) { + pixels[x] = new Pixel(); + } + +} + +void pushPixels(int pushX, int pushY) { + // Apply force to each pixel outwards from the mouse + for (int i = 0; i < NPIXELS; i++) { + // Get the direction + float dx = pixels[i]->x - pushX; + float dy = pixels[i]->y - pushY; + float mag = sqrtf(dx*dx + dy*dy); + dx /= mag; + dy /= mag; + + // Apply force. + pixels[i]->accel(dx * 10.0, dy * 10.0); + } +} + +void engine() { + // Clear the canvas + glClear(GL_COLOR_BUFFER_BIT); + + // Update cursor + vertices[0] = mx - 5; + vertices[1] = my - 5; + vertices[3] = mx + 5; + vertices[4] = my - 5; + vertices[6] = mx + 5; + vertices[7] = my + 5; + vertices[9] = mx - 5; + vertices[10] = my + 5; + glDrawArrays(GL_LINE_LOOP, 0, 4); + + // Update all pixel objects + for (int i = 0; i < NPIXELS; i++) { + // Get distance + float dx = mx - pixels[i]->x; + float dy = my - pixels[i]->y; + float d = sqrtf(dx*dx + dy*dy); + + // Apply acceleration to pixels towards the mouse cursor + float ax = dx / 50.0; + float ay = dy / 50.0; + + // Apply a drag proportional to the distance from the cursor + if (d > 25.0) { + ax += -pixels[i]->vx * (d - 25.0) / 1000.0; + ay += -pixels[i]->vy * (d - 25.0) / 1000.0; + } + + // And a "chaotic" acceleration inversely proportional + // to the distance from the cursor + ax += ((rand() / (float)RAND_MAX) * 40.0 - 20.0) / d; + ay += ((rand() / (float)RAND_MAX) * 40.0 - 20.0) / d; + + pixels[i]->accel(ax,ay); + pixels[i]->update(); + pixels[i]->draw(&vertices[i*6]); + } + glDrawArrays(GL_LINES, 0, NPIXELS * 2); +} + +int main(int argc, char** argv) +{ + // Initialize the SDL library with the Video subsystem + SDL_Init(SDL_INIT_VIDEO); + + // start the PDL library + PDL_Init(0); + + // Tell it to use OpenGL ES version 1.1 + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1); + + // Set the video mode to full screen with OpenGL-ES support + screen = SDL_SetVideoMode(320, 400, 0, SDL_OPENGL); + if (!screen) { + printf("Could not initialize video!\n"); + exit(1); + } + printf("width: %d, height, %d\n", screen->w, screen->h); + +#if WIN32 + // Load the desktop OpenGL-ES emulation library + _dgles_load_library(NULL, proc_loader); +#endif + + init(); + + // Event descriptor + SDL_Event Event; + int app_is_active = 1; + Uint32 last = SDL_GetTicks(); + int fps_counter = 0; + + do { + // Process the events + if (!app_is_active) + SDL_WaitEvent(NULL); + while (SDL_PollEvent(&Event)) { + switch (Event.type) { + // List of keys that have been pressed + case SDL_KEYDOWN: + switch (Event.key.keysym.sym) { + // Escape forces us to quit the app + // this is also sent when the user makes a back gesture + case SDLK_ESCAPE: + Event.type = SDL_QUIT; + break; + default: + break; + } + break; + case SDL_MOUSEMOTION: + mx = Event.motion.x; + my = Event.motion.y; + break; + case SDL_MOUSEBUTTONDOWN: + pushPixels(Event.button.x, Event.button.y); + case SDL_ACTIVEEVENT: + if (Event.active.state == SDL_APPACTIVE) + app_is_active = Event.active.gain; + break; + default: + break; + } + } + + engine(); + glFlush(); + SDL_GL_SwapBuffers(); + + // Calculate FPS + fps_counter++; + Uint32 now = SDL_GetTicks(); + if (now - last > 1000) { + printf("%d FPS\n", fps_counter); + fps_counter = 0; + last = now; + } + } while (Event.type != SDL_QUIT); + // We exit anytime we get a request to quit the app + + // Cleanup + PDL_Quit(); + SDL_Quit(); + + return 0; +} diff --git a/swarm.sln b/swarm.sln new file mode 100644 index 0000000..28af9aa --- /dev/null +++ b/swarm.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 10.00 +# Visual Studio 2008 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "swarm", "swarm.vcproj", "{084AA5FB-DC4E-4A84-B198-19448669AD25}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {084AA5FB-DC4E-4A84-B198-19448669AD25}.Debug|Win32.ActiveCfg = Debug|Win32 + {084AA5FB-DC4E-4A84-B198-19448669AD25}.Debug|Win32.Build.0 = Debug|Win32 + {084AA5FB-DC4E-4A84-B198-19448669AD25}.Release|Win32.ActiveCfg = Release|Win32 + {084AA5FB-DC4E-4A84-B198-19448669AD25}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/swarm.vcproj b/swarm.vcproj new file mode 100644 index 0000000..d9502e0 --- /dev/null +++ b/swarm.vcproj @@ -0,0 +1,210 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- 2.25.1