转载记录备用:SDL模拟分子运动 http://blog.csdn.net/rcfalcon/article/details/5265227

 
 
#pragma comment(lib,"SDL/SDL.lib")  
#pragma comment(lib,"SDL/SDLmain.lib")  
  
#include"include/SDL.h"  
  
#include <stdlib.h>   
#include <stdio.h>   
#include <time.h>  
  
#include<iostream>  
using namespace std;  
  
  
SDL_Surface *screen;  
SDL_Surface *image1;  
SDL_Surface *image2;  
SDL_Surface *image3;  
  
int const MAX = 30;  
  
int const fps = 75;  
int const datarefresh = 1;  
  
SDL_Rect offsect[MAX];  
  
SDL_Thread *thread = NULL;  
  
void work();  
void cls();  
void drawscreen();  
void init();  
void quit();  
int my_thread( void *data );  
  
  
int main(int argc, char **argv)  
{  
 init();  
  
 while(1)  
 {  
  cls();  
  SDL_Delay( 1000 / fps );  
  drawscreen();  
 }  
  
 quit();  
 return 0;  
}  
  
void init()  
{  
 SDL_Init(SDL_INIT_VIDEO);  
 screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);  
 srand( time( 0 ) );  
  
 image1 = SDL_LoadBMP("res/test3.bmp");  
 image2 = SDL_LoadBMP("res/test2.bmp");  
 image3 = SDL_LoadBMP("res/back.bmp");  
  
 Uint32 colorkey = SDL_MapRGB( image1->format, 0xFF, 0xFF, 0xFF );  
 SDL_SetColorKey( image1, SDL_SRCCOLORKEY, colorkey );  
 SDL_SetColorKey( image2, SDL_SRCCOLORKEY, colorkey );  
 SDL_SetColorKey( image3, SDL_SRCCOLORKEY, colorkey );  
  
 for( int i=0; i < MAX; i++ )  
 {  
  offsect[i].x = 300;  
  offsect[i].y = 200;  
  offsect[i].w = image1->w;  
  offsect[i].h = image1->h;  
 }  
  
 thread = SDL_CreateThread( my_thread, NULL );  
}  
  
void cls()  
{  
 SDL_Rect tmp;  
 tmp.x = 0;  
 tmp.y = 0;  
 tmp.w = 640;  
 tmp.h = 480;  
  
 SDL_BlitSurface( image3, 0, screen, &tmp );  
}  
  
void drawscreen()  
{  
 SDL_UpdateRect(screen, 0, 0, 640, 480);  
}  
  
void work()  
{  
 static bool turn = false;   
 SDL_Rect offsectnew;  
  
  
 for( int i=0; i < MAX; i++ )  
 {   
  offsectnew = offsect[i];  
  int x_delta,y_delta;  
    
  do  
  {  
   x_delta = ( (rand()%3) -1 )*1;   
   y_delta = ( (rand()%3) -1 )*1;  
  }  
  while( (offsectnew.x+x_delta)<0 && (offsectnew.x+x_delta)>640   
   && (offsectnew.y+y_delta)<0 && (offsectnew.y+y_delta)>480 );  
    
  offsectnew.x += x_delta ;  
  offsectnew.y += y_delta ;  
    
  turn = !turn;  
  SDL_Surface *image;  
    
    
  if(turn)  
   image = image1;  
  else  
   image = image2;  
    
    
  SDL_BlitSurface(image, 0, screen, &offsectnew);  
    
    
  offsect[i] = offsectnew;  
 }  
}  
  
  
int my_thread( void *data )  
{  
    while( 1 )  
    {  
  work();  
  SDL_Delay( datarefresh );  
    }  
      
    return 0;      
}  
  
void quit()  
{  
 SDL_FreeSurface(image1);  
 SDL_FreeSurface(image2);  
 SDL_FreeSurface(image3);  
 SDL_KillThread( thread );  
 SDL_Quit();  
}  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值