annotate projects/tank_wars/vector_graphics.cpp @ 45:579c86316008 newapi

Major API overhaul. Moved to a single data structure for handling render functions. Functionally, generally similar except for scheduling within PRU loop function, which now uses interrupts from the PRU rather than polling. This requires an updated kernel.
author andrewm
date Thu, 28 May 2015 14:35:55 -0400
parents 49f22e1246b2
children
rev   line source
andrewm@10 1 /*
andrewm@10 2 * vector_graphics.cpp
andrewm@10 3 *
andrewm@10 4 * Created on: Nov 10, 2014
andrewm@10 5 * Author: parallels
andrewm@10 6 */
andrewm@10 7
andrewm@10 8 #include <cmath>
andrewm@10 9
andrewm@10 10 // Draw a line between two points at a specified rate in
andrewm@10 11 // pixels per buffer sample. Indicate maximum available space.
andrewm@10 12 // Returns space used
andrewm@10 13 int renderLine(float x1, float y1, float x2, float y2, float speed,
andrewm@10 14 float *buffer, int maxLength) {
andrewm@10 15 // Figure out length of line and therefore how many samples
andrewm@10 16 // are needed to represent it based on the speed (rounded to nearest int)
andrewm@10 17 float totalLineLength = sqrtf((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
andrewm@10 18 int samplesNeeded = floorf(totalLineLength / speed + 0.5);
andrewm@10 19
andrewm@10 20 // Now render into the buffer
andrewm@10 21 int length = 0;
andrewm@10 22 float scaleFactor = 1.0f / samplesNeeded;
andrewm@10 23 for(int n = 0; n < samplesNeeded; n++) {
andrewm@10 24 if(length >= maxLength - 1)
andrewm@10 25 return length;
andrewm@10 26 // X coordinate
andrewm@10 27 *buffer++ = x1 + (float)n * scaleFactor * (x2 - x1);
andrewm@10 28 // Y coordinate
andrewm@10 29 *buffer++ = y1 + (float)n * scaleFactor * (y2 - y1);
andrewm@10 30 length += 2;
andrewm@10 31 }
andrewm@10 32
andrewm@10 33 return length;
andrewm@10 34 }
andrewm@10 35
andrewm@10 36 // Draw an arc around a centre point at a specified rate of pixels
andrewm@10 37 // per buffer sample. Indicate maximum available space.
andrewm@10 38 // Returns space used
andrewm@10 39 int renderArc(float x, float y, float radius, float thetaMin, float thetaMax,
andrewm@10 40 float speed, float *buffer, int maxLength) {
andrewm@10 41 // Figure out circumference of arc and therefore how many samples
andrewm@10 42 // are needed to represent it based on the speed (rounded to nearest int)
andrewm@10 43 float circumference = (thetaMax - thetaMin) * radius;
andrewm@10 44 int samplesNeeded = floorf(circumference / speed + 0.5);
andrewm@10 45
andrewm@10 46 // Now render into the buffer
andrewm@10 47 int length = 0;
andrewm@10 48 float scaleFactor = 1.0f / samplesNeeded;
andrewm@10 49 for(int n = 0; n < samplesNeeded; n++) {
andrewm@10 50 if(length >= maxLength - 1)
andrewm@10 51 return length;
andrewm@10 52 // Get current angle
andrewm@10 53 float theta = thetaMin + (float)n * scaleFactor * (thetaMax - thetaMin);
andrewm@10 54
andrewm@10 55 // Convert polar to cartesian coordinates
andrewm@10 56 *buffer++ = x + radius * cosf(theta);
andrewm@10 57 *buffer++ = y + radius * sinf(theta);
andrewm@10 58
andrewm@10 59 length += 2;
andrewm@10 60 }
andrewm@10 61
andrewm@10 62 return length;
andrewm@10 63 }
andrewm@10 64
andrewm@10 65 // Draw a single point for a specified number of frames
andrewm@10 66 void renderPoint(float x, float y, float *buffer, float length) {
andrewm@10 67 while(length > 0) {
andrewm@10 68 *buffer++ = x;
andrewm@10 69 *buffer++ = y;
andrewm@10 70 length--;
andrewm@10 71 }
andrewm@10 72 }