Advertisement
Guest User

Untitled

a guest
May 22nd, 2012
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | None | 0 0
  1. #include <string.h>
  2. #include <SDL/SDL.h>
  3. #include <SDL/SDL_gfxPrimitives.h>
  4. #include <iostream>
  5. #include <vector>
  6. #include <math.h>
  7.  
  8. #define PI 3.14159265
  9.  
  10. using namespace std;
  11.  
  12. const int WINDOW_WIDTH = 640;
  13. const int WINDOW_HEIGHT = 480;
  14. const char* WINDOW_TITLE = "SDL POLY TEST";
  15.  
  16. class CPoly {
  17.   private:
  18.     vector<int> points;
  19.     int* vpBuffer = (int*) calloc(50,sizeof(int));
  20.     int rotang;
  21.     int xoffs;
  22.     int yoffs;
  23.   public:
  24.     CPoly (vector<int> tpoints, int rot, int xoff, int yoff) {
  25.       free(vpBuffer);
  26.       points = tpoints;
  27.       rotang = rot;
  28.       xoffs = xoff;
  29.       yoffs = yoff;
  30.     }
  31.     void setRot (int newrot) {
  32.       rotang=newrot;
  33.     }
  34.     void render (SDL_Surface* screen) {
  35.       int wang=rotang*PI/180;
  36.       for(int i=2;i<points.size();i+=2) {
  37.         int xa=points[i-2];
  38.         int ya=points[i-1];
  39.         int xb=points[i];
  40.         int yb=points[i+1];
  41.         lineRGBA(screen,
  42.                xoffs+((xa*cos(wang))-(ya*sin(wang))),
  43.                yoffs+((xa*sin(wang))+(ya*cos(wang))),
  44.                xoffs+((xb*cos(wang))-(yb*sin(wang))),
  45.                yoffs+((xb*sin(wang))+(yb*cos(wang))),
  46.                255,255,255,255);
  47.       }
  48.     }
  49.    
  50. };
  51.  
  52. vector<int> makeRect(int width,int height) {
  53.   vector<int> vpoints;
  54.   vpoints.push_back(-width/2);
  55.   vpoints.push_back(-height/2);
  56.   vpoints.push_back(-width/2);
  57.   vpoints.push_back(height/2);
  58.   vpoints.push_back(width/2);
  59.   vpoints.push_back(height/2);
  60.   vpoints.push_back(width/2);
  61.   vpoints.push_back(-height/2);
  62.   vpoints.push_back(-width/2);
  63.   vpoints.push_back(-height/2);
  64.   return vpoints;
  65. }
  66. vector<int> makeCircle(int r,int depth) {
  67.   vector<int> vpoints;
  68.   int x,y;
  69.   int thang;
  70.   for(int i=0;i<360/(360/depth);i++) {
  71.     thang=i*(360/depth)*PI/180;
  72.     x=r*cos(thang);
  73.     y=r*sin(thang);
  74.     vpoints.push_back(x);
  75.     vpoints.push_back(y);
  76.   }
  77.   vpoints.push_back(vpoints[0]);
  78.   vpoints.push_back(vpoints[1]);
  79.   return vpoints;
  80. }
  81.  
  82. int main(int argc, char **argv)
  83. {
  84.    SDL_Init( SDL_INIT_VIDEO );
  85.    SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0,
  86.       SDL_HWSURFACE | SDL_DOUBLEBUF );
  87.    SDL_WM_SetCaption( WINDOW_TITLE, 0 );
  88.    SDL_Event event;
  89.    
  90.    
  91.    
  92.    
  93.    int myints[] = {0,0,0,50,50,50,50,0,25,-25,0,0};
  94.    vector<int> vertices (myints,myints+sizeof(myints)/sizeof(int));
  95.    CPoly derpity (vertices,0,200,200);
  96.    
  97.    CPoly derpette (makeCircle(50,40),0,400,250);
  98.    
  99.    int step=0;
  100.    bool gameRunning = true;
  101.    while (gameRunning) {
  102.       step++;
  103.       if (SDL_PollEvent(&event)) {
  104.          if (event.type == SDL_QUIT) {
  105.             gameRunning = false;
  106.          }
  107.       }
  108.       SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
  109.      
  110.       derpity.render(screen);
  111.       derpette.render(screen);
  112.      
  113.       derpette.setRot(step%360);
  114.       derpity.setRot(step%360);
  115.      
  116.       SDL_Flip(screen);
  117.    }
  118.  
  119.    SDL_Quit();
  120.  
  121.    return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement