// image calculation
// load frame from video
void VideoFFmpeg::calcImage (unsigned int texId, double ts)
{
if (m_status == SourcePlaying)
{
// get actual time
double startTime = PIL_check_seconds_timer();
double actTime;
// timestamp passed from audio actuators can sometimes be slightly negative
if (m_isFile && ts >= -0.5)
{
// allow setting timestamp only when not streaming
actTime = ts;
if (actTime * actFrameRate() < m_lastFrame)
{
// user is asking to rewind, force a cache clear to make sure we will do a seek
// note that this does not decrement m_repeat if ts didn't reach m_range[1]
stopCache();
}
}
else
{
if (m_lastFrame == -1 && !m_isFile)
m_startTime = startTime;
actTime = startTime - m_startTime;
}
// if video has ended
if (m_isFile && actTime * m_frameRate >= m_range[1])
{
// in any case, this resets the cache
stopCache();
// if repeats are set, decrease them
if (m_repeat > 0)
--m_repeat;
// if video has to be replayed
if (m_repeat != 0)
{
// reset its position
actTime -= (m_range[1] - m_range[0]) / m_frameRate;
m_startTime += (m_range[1] - m_range[0]) / m_frameRate;
}
// if video has to be stopped, stop it
else
{
m_status = SourceStopped;
return;
}
}
// actual frame
long actFrame = (m_isImage) ? m_lastFrame+1 : long(actTime * actFrameRate());
// if actual frame differs from last frame
if (actFrame != m_lastFrame)
{
AVFrame* frame;
// get image
if ((frame = grabFrame(actFrame)) != NULL)
{
if (!m_isFile && !m_cacheStarted)
{
// streaming without cache: detect synchronization problem
double execTime = PIL_check_seconds_timer() - startTime;
if (execTime > 0.005)
{
// exec time is too long, it means that the function was blocking
// resynchronize the stream from this time
m_startTime += execTime;
}
}
// save actual frame
m_lastFrame = actFrame;
// init image, if needed
init(short(m_codecCtx->width), short(m_codecCtx->height));
// process image
process((BYTE*)(frame->data[0]));
// finished with the frame, release it so that cache can reuse it
releaseFrame(frame);
// in case it is an image, automatically stop reading it
if (m_isImage)
{
m_status = SourceStopped;
// close the file as we don't need it anymore
release();
}
} else if (m_isStreaming)
{
// we didn't get a frame and we are streaming, this may be due to
// a delay in the network or because we are getting the frame too fast.
// In the later case, shift time by a small amount to compensate for a drift
m_startTime += 0.001;
}
}
}
}
LRESULT CALLBACK Win32Assistant::WindowProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
Win32Assistant* assistant;
if (IsWindowUnicode (hWnd))
assistant = (Win32Assistant*)GetWindowLongPtrW (hWnd, 0);
else
assistant = (Win32Assistant*)GetWindowLongPtrA (hWnd, 0);
switch (message)
{
case WM_ACTIVATEAPP:
if ((assistant != 0))
{
if (wParam)
{
assistant->ApplicationActive = true;
}
else
{
assistant->ApplicationActive = false;
}
}
break;
case WM_ACTIVATE:
if ((assistant != 0))
{
iEventOutlet* outlet = assistant->GetEventOutlet();
if (LOWORD(wParam) != WA_INACTIVE)
outlet->Broadcast (assistant->FocusGained, 1);
else
outlet->Broadcast (assistant->FocusLost, 0);
}
break;
case WM_CREATE:
{
CreateInfo* ci;
if (IsWindowUnicode (hWnd))
{
ci = (CreateInfo*)((LPCREATESTRUCTW)lParam)->lpCreateParams;
SetWindowLongPtrW (hWnd, 0, (LONG_PTR)ci->assistant);
SetWindowLongPtrW (hWnd, sizeof (LONG_PTR), (LONG_PTR)ci->canvas);
}
else
{
ci = (CreateInfo*)((LPCREATESTRUCTA)lParam)->lpCreateParams;
SetWindowLongPtrA (hWnd, 0, (LONG_PTR)ci->assistant);
SetWindowLongPtrA (hWnd, sizeof (LONG_PTR), (LONG_PTR)ci->canvas);
}
// a window is created. Hide the console window, if requested.
if (ci->assistant->is_console_app &&
!ci->assistant->console_window)
{
ci->assistant->DisableConsole ();
}
}
break;
case WM_SYSCOMMAND:
if (wParam == SC_CLOSE)
{
PostQuitMessage (0);
return TRUE;
}
break;
case WM_SYSCHAR:
case WM_CHAR:
case WM_UNICHAR:
case WM_DEADCHAR:
case WM_SYSDEADCHAR:
case WM_IME_COMPOSITION:
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
case WM_KEYUP:
case WM_SYSKEYUP:
{
if (assistant != 0)
{
if (assistant->HandleKeyMessage (hWnd, message, wParam, lParam))
{
return 0;
}
}
break;
}
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
{
if (assistant != 0)
{
const int buttonNum = (message == WM_LBUTTONDOWN) ? csmbLeft :
(message == WM_RBUTTONDOWN) ? csmbRight : csmbMiddle;
if (assistant->mouseButtons == 0) SetCapture (hWnd);
assistant->mouseButtons |= 1 << (buttonNum - csmbLeft);
iEventOutlet* outlet = assistant->GetEventOutlet();
outlet->Mouse (buttonNum, true,
short (LOWORD (lParam)), short (HIWORD (lParam)));
}
return TRUE;
}
//.........这里部分代码省略.........
开发者ID:garinh,项目名称:cs,代码行数:101,代码来源:win32.cpp
示例3: err_when
//--------- Begin of function UnitArray::move_to_now ---------//
//
// Order the unit to move to a specific location following the
// shortest path.
//
// <int> destXLoc, destYLoc - the location of the destination.
// <short*> selectedUnitArray - an array of recno of selected units.
// <int> selectedCount - no. of selected units.
//
void UnitArray::move_to_now(int destXLoc, int destYLoc, short* selectedUnitArray, int selectedCount)
{
err_when(destXLoc<0 || destYLoc<0 || destXLoc>=MAX_WORLD_X_LOC || destYLoc>=MAX_WORLD_Y_LOC);
err_when( selectedCount > 10000 );
//------------ define vars -----------------------//
int unprocessCount;// = selectedCount; // num. of unprocessed sprite
int k; // for counting
short vecX, vecY; // used to reset x, y
short oddCount, evenCount;
int j;
Unit* unitPtr = unit_array[selectedUnitArray[0]];
DWORD curGroupId = unitPtr->unit_group_id;
int mobileType = unitPtr->mobile_type;
Location *worldLocMatrix=world.loc_matrix, *locPtr;
//int sizeOneSelectedCount=0, sizeTwoSelectedCount=0;
int sizeOneSelectedCount = selectedCount;
//---------- set Unit::unit_group_id and count the unit by size ----------//
int i;
for( i=0 ; i<selectedCount ; i++ )
{
unitPtr = operator[](selectedUnitArray[i]);
err_when(unitPtr->cur_action==SPRITE_IDLE && (unitPtr->cur_x!=unitPtr->next_x || unitPtr->cur_y!=unitPtr->next_y));
err_when(unitPtr->action_para); // action_para should be set to zero in move_to()
if(unitPtr->cur_action==SPRITE_ATTACK)
unitPtr->stop();
err_when(unitPtr->cur_action==SPRITE_ATTACK && unitPtr->action_para==0);
if(unitPtr->cur_action==SPRITE_IDLE)
unitPtr->set_ready();
/*switch(unitPtr->sprite_info->loc_width)
{
case 1: sizeOneSelectedCount++;
break;
case 2: sizeTwoSelectedCount++;
break;
default: err_here();
break;
}*/
}
unprocessCount = sizeOneSelectedCount;
//---- construct array to store size one selected unit ----//
short* selectedSizeOneUnitArray;
if(sizeOneSelectedCount)
{
selectedSizeOneUnitArray = (short*)mem_add(sizeof(short)*sizeOneSelectedCount);
memset(selectedSizeOneUnitArray, 0, sizeof(short)*sizeOneSelectedCount);
for(i=0, k=0; i<selectedCount && unprocessCount; i++)
{
unitPtr = operator[](selectedUnitArray[i]);
if(unitPtr->sprite_info->loc_width==1)
{
selectedSizeOneUnitArray[k++] = selectedUnitArray[i];
unprocessCount--;
}
}
}
unprocessCount = sizeOneSelectedCount;
//----------- variables initialization ---------------//
int destX, destY;
if(mobileType==UNIT_LAND)
{
x = destX = destXLoc;
y = destY = destYLoc;
move_scale = 1;
}
else // UNIT_AIR, UNIT_SEA
{
x = destX = (destXLoc/2)*2;
y = destY = (destYLoc/2)*2;
move_scale = 2;
}
//if(sizeOneSelectedCount)
//{
//----- initialize parameters and construct data structure -----//
oddCount =1;
evenCount = 3;
square_size = not_tested_loc = lower_right_case = upper_left_case = 0;
distance = (int*)mem_add(sizeof(int)*sizeOneSelectedCount); // used in the function construct_sorted_array and this function,
memset(distance, 0, sizeof(int)*sizeOneSelectedCount); // and allocate/free the memory in this function
sorted_distance = (int*)mem_add(sizeof(int)*sizeOneSelectedCount);
//.........这里部分代码省略.........
开发者ID:brianV,项目名称:7kaa,代码行数:101,代码来源:OUNITAM.cpp
示例4: tmp
inline void Scene::PhongColor (float px, float py, float pz,
float nx, float ny, float nz,
Material* m,
short& rr, short& gg, short& bb)
{
const float Eps = 1e-5f;
// check for NULL material pointer - take scenes default material
if ( ! m ) m = &mat;
// check for image texture mapping
if (m->use_image_texture) {
Matpack.Error("Scene::PhongColor: image textures not yet available");
rr = gg = bb = 0;
return;
}
// check for normal texturing function - apply to normal vector
if (m->use_normal_texture) {
Vector3D tmp(nx,ny,nz);
(*m).normal_texture(tmp);
nx = tmp.x; ny = tmp.y; nz = tmp.z;
}
// normalization of normal vector
float sn = hypot(nx,ny,nz);
if (sn) { nx /= sn; ny /= sn; nz /= sn; }
// normalization of coordinate vector of surface point
float sp = hypot(px,py,pz), pxn = 0, pyn = 0, pzn = 0;
if (sp) { pxn = px/sp; pyn = py/sp; pzn = pz/sp; }
float r = 0,
g = 0,
b = 0,
spec = 0,
amb = m->ambient, // ambient light contribution
spc = m->specular; // specular light contribution
// loop through list of lights
for (Light *l = FirstLight(); l; NextLight(l) ) {
// distance of light to point on surface: sl = |light-point|
float sxp = l->x - px,
syp = l->y - py,
szp = l->z - pz,
sl = hypot(sxp,syp,szp),
cs = (sl == 0.0) ? 0.0 : (nx*sxp+ny*syp+nz*szp)/sl;
// If cs is negative then we look at the back side, if it is positive
// then the front side is visible. The side is determined by the orientation
// of the vertices in the facet (counter clockwise = front side).
if (cs < 0.0) cs = 0;
// ambient light plus diffuse reflection according to Lambert's law
float light = (1.0 - amb) * cs + amb;
// add specular reflection according to Bui-Tuong Phong's model
if (spc > Eps) {
if (sl && sp && sn) {
float qx = sxp/sl - pxn,
qy = syp/sl - pyn,
qz = szp/sl - pzn,
sq = hypot(qx,qy,qz);
if (sq != 0.0) {
float csa2 = (qx*nx+qy*ny+qz*nz) / sq,
csa = 2.0 * csa2*csa2 - 1.0;
spec = (csa > Eps) ? spc * pow(csa,m->exponent) : 0.0;
} else
spec = 0.0;
} else
spec = 0.0;
}
// intensity factor of light
light *= l->intensity;
// now multiply light with surface color
// evaluate a texturing function if neccessary
if (m->use_color_texture) {
Vector3D tmp = Qinv * Vector3D(px,py,pz); // backtrafo to user coord.
(*m).color_texture(tmp);
r += light * l->red * tmp.x + spec;
g += light * l->green * tmp.y + spec;
b += light * l->blue * tmp.z + spec;
} else {
r += light * l->red * m->color.red + spec;
g += light * l->green * m->color.green + spec;
b += light * l->blue * m->color.blue + spec;
}
}
// Fog can be simulated by mixing the background color to the surface color.
// With increasing distance of the camera from the surface point (p) an
// exponentially increasing contribution of the background color
// should be mixed to the surface color.
//
// But this model is not flexible enough. A much better fog is a
// parametrization using the hyperbolic tangent as blending function.
//.........这里部分代码省略.........
std::vector<bool> Halite::processNextFrame(std::vector<bool> alive) {
//Update alive frame counts
for(unsigned char a = 0; a < number_of_players; a++) if(alive[a]) alive_frame_count[a]++;
//Create threads to send/receive data to/from players. The threads should return a float of how much time passed between the end of their message being sent and the end of the AI's message being sent.
std::vector< std::future<unsigned int> > frameThreads(std::count(alive.begin(), alive.end(), true));
unsigned char threadLocation = 0; //Represents place in frameThreads.
//Figure out how long each AI is permitted to respond without penalty in milliseconds.
std::vector<int> allowableTimesToRespond(number_of_players);
const int BOT_FRAME_TIMEOUT_MILLIS = 50 + ((game_map.map_width * game_map.map_height) / 2);
for(unsigned char a = 0; a < number_of_players; a++) allowableTimesToRespond[a] = BOT_FRAME_TIMEOUT_MILLIS;
//Stores the messages sent by bots this frame
for(unsigned char a = 0; a < number_of_players; a++) {
if(alive[a]) {
frameThreads[threadLocation] = std::async(&Networking::handleFrameNetworking, &networking, allowableTimesToRespond[a], a + 1, game_map, &player_moves[a]);
threadLocation++;
}
}
std::vector< std::vector<unsigned char> > moveDirections(game_map.map_height, std::vector<unsigned char>(game_map.map_width, 0));
//Join threads. Figure out if the player responded in an allowable amount of time or if the player has timed out.
std::vector<unsigned short> permissibleTime(number_of_players, false);
threadLocation = 0; //Represents place in frameThreads.
for(unsigned char a = 0; a < number_of_players; a++) {
if(alive[a]) {
unsigned short millis = frameThreads[threadLocation].get();
if(millis < BOT_FRAME_TIMEOUT_MILLIS) {
permissibleTime[a] = true;
}
// There was an exception in the networking thread or the player timed out. Either way, kill their thread
else {
if(!program_output_style) std::cout << player_names[a] << " timed out\n";
permissibleTime[a] = false;
networking.killPlayer(a + 1);
}
threadLocation++;
total_response_time[a] += millis;
}
}
std::vector< std::map<hlt::Location, unsigned char> > pieces(number_of_players);
//For each player, use their moves to create the pieces map.
for(unsigned char a = 0; a < number_of_players; a++) if(alive[a]) {
//Add in pieces according to their moves. Also add in a second piece corresponding to the piece left behind.
for(auto b = player_moves[a].begin(); b != player_moves[a].end(); b++) if(game_map.inBounds(b->loc) && game_map.getSite(b->loc, STILL).owner == a + 1) {
if(b->dir == STILL) {
if(game_map.getSite(b->loc, STILL).strength + game_map.getSite(b->loc, STILL).production <= 255) game_map.getSite(b->loc, STILL).strength += game_map.getSite(b->loc, STILL).production;
else game_map.getSite(b->loc, STILL).strength = 255;
//Update full still count
full_still_count[a]++;
//Add to full production
full_production_count[a] += game_map.getSite(b->loc, STILL).production;
}
//Update full caridnal count.
else full_cardinal_count[a]++;
//Update moves
moveDirections[b->loc.y][b->loc.x] = b->dir;
hlt::Location newLoc = game_map.getLocation(b->loc, b->dir);
if(pieces[a].count(newLoc)) {
if(short(pieces[a][newLoc]) + game_map.getSite(b->loc, STILL).strength <= 255) pieces[a][newLoc] += game_map.getSite(b->loc, STILL).strength;
else pieces[a][newLoc] = 255;
}
else {
pieces[a].insert(std::pair<hlt::Location, unsigned char>(newLoc, game_map.getSite(b->loc, STILL).strength));
}
//Add in a new piece with a strength of 0 if necessary.
if(!pieces[a].count(b->loc)) {
pieces[a].insert(std::pair<hlt::Location, unsigned char>(b->loc, 0));
}
//Erase from the game map so that the player can't make another move with the same piece.
game_map.getSite(b->loc, STILL).owner = 0;
game_map.getSite(b->loc, STILL).strength = 0;
}
}
//Add in all of the remaining pieces whose moves weren't specified.
for(unsigned short a = 0; a < game_map.map_height; a++) for(unsigned short b = 0; b < game_map.map_width; b++) {
hlt::Location l = { b, a };
hlt::Site & s = game_map.getSite(l, STILL);
if(s.owner != 0) {
if(short(s.strength) + s.production <= 255) {
s.strength += s.production;
}
else s.strength = 255;
if(pieces[s.owner - 1].count(l)) {
if(short(pieces[s.owner - 1][l]) + s.strength <= 255) pieces[s.owner - 1][l] += s.strength;
else pieces[s.owner - 1][l] = 255;
}
else {
pieces[s.owner - 1].insert(std::pair<hlt::Location, unsigned char>(l, s.strength));
}
//.........这里部分代码省略.........
请发表评论