/* Sort the input objects and recalculate the indices for each input. */ static void SortDevObjects(SDL_Joystick *joystick) { input_t *inputs = joystick->hwdata->Inputs; int nButtons = 0; int nHats = 0; int nAxis = 0; int n; SDL_qsort(inputs, joystick->hwdata->NumInputs, sizeof(input_t), SortDevFunc); for (n = 0; n < joystick->hwdata->NumInputs; n++) { switch (inputs[n].type) { case BUTTON: inputs[n].num = nButtons; nButtons++; break; case HAT: inputs[n].num = nHats; nHats++; break; case AXIS: inputs[n].num = nAxis; nAxis++; break; } } }
int SDLFrontend::renderFilledPolygon (int *vx, int *vy, int n, const Color& color) { if (!vx || !vy || n < 3) return -1; ScopedArrayPtr<int> ints(new int[n]); int miny = vy[0]; int maxy = vy[0]; for (int i = 1; i < n; i++) { if (vy[i] > maxy) maxy = vy[i]; if (vy[i] < miny) miny = vy[i]; } int result = 0; for (int y = miny; y <= maxy; y++) { int num_ints = 0; for (int i = 0; i < n; i++) { int ind1, ind2; if (i == 0) { ind1 = n - 1; ind2 = 0; } else { ind1 = i - 1; ind2 = i; } int x1; int x2; int y1 = vy[ind1]; int y2 = vy[ind2]; if (y1 < y2) { x1 = vx[ind1]; x2 = vx[ind2]; } else if (y1 > y2) { y2 = vy[ind1]; y1 = vy[ind2]; x2 = vx[ind1]; x1 = vx[ind2]; } else { continue; } if ((y >= y1 && y < y2) || (y == maxy && y <= y2 && y > y1)) { ints[num_ints++] = ((y - y1) * (x2 - x1) / (y2 - y1)) + x1; } } SDL_qsort(ints, num_ints, sizeof(int), sortRenderFilledPolygon); for (int i = 0; i < num_ints; i += 2) { const int start = ints[i]; const int end = ints[i + 1]; result |= SDL_RenderDrawLine(_renderer, start, y, end, y); } } return result; }
/* if requested bpp is not found the mode with closest bpp is returned */ int get_mode_any_format(int width, int height, int bpp) { int i, closest, delta, min_delta; if (PgGetVideoModeList(&mode_list) < 0) { return -1; } SDL_qsort(mode_list.modes, mode_list.num_modes, sizeof(unsigned short), compare_modes_by_res); for (i = 0; i < mode_list.num_modes; i++) { if (PgGetVideoModeInfo(mode_list.modes[i], &mode_info) < 0) { return 0; } if ((mode_info.width == width) && (mode_info.height == height)) { break; } } if (i < mode_list.num_modes) { /* get closest bpp */ closest = i++; if (mode_info.bits_per_pixel == bpp) { return mode_list.modes[closest]; } min_delta = abs(mode_info.bits_per_pixel - bpp); while (1) { if (PgGetVideoModeInfo(mode_list.modes[i], &mode_info) < 0) { return 0; } if ((mode_info.width != width) || (mode_info.height != height)) { break; } else { if (mode_info.bits_per_pixel == bpp) { closest = i; break; } else { delta = abs(mode_info.bits_per_pixel - bpp); if (delta < min_delta) { closest = i; min_delta = delta; } i++; } } } return mode_list.modes[closest]; } return 0; }
static void test_sort(const char *desc, int *nums, const int arraylen) { int i; int prev; SDL_Log("test: %s arraylen=%d", desc, arraylen); SDL_qsort(nums, arraylen, sizeof (nums[0]), num_compare); prev = nums[0]; for (i = 1; i < arraylen; i++) { const int val = nums[i]; if (val < prev) { SDL_Log("sort is broken!"); return; } prev = val; } }
void FULLSCREEN_BuildModeList(_THIS) { _kernel_swi_regs regs; char *enumInfo = NULL; char *enum_ptr; int *blockInfo; int j; int num_modes; regs.r[0] = 2; regs.r[2] = 0; regs.r[6] = 0; regs.r[7] = 0; _kernel_swi(OS_ScreenMode, ®s, ®s); num_modes = -regs.r[2]; this->info.video_mem = regs.r[5]/1024; enumInfo = (unsigned char *)SDL_malloc(-regs.r[7]); if (enumInfo == NULL) { SDL_OutOfMemory(); return; } regs.r[2] = 0; regs.r[6] = (int)enumInfo; regs.r[7] = -regs.r[7]; _kernel_swi(OS_ScreenMode, ®s, ®s); enum_ptr = enumInfo; for (j =0; j < num_modes;j++) { blockInfo = (int *)enum_ptr; if ((blockInfo[1] & 255) == 1) { switch(blockInfo[4]) { case 3: FULLSCREEN_AddMode(this, 8, blockInfo[2], blockInfo[3]); break; case 4: FULLSCREEN_AddMode(this, 15, blockInfo[2], blockInfo[3]); break; case 5: FULLSCREEN_AddMode(this, 32, blockInfo[2], blockInfo[3]); break; } } enum_ptr += blockInfo[0]; } SDL_free(enumInfo); for ( j=0; j<NUM_MODELISTS; ++j ) { if ( SDL_nummodes[j] > 0 ) { SDL_qsort(SDL_modelist[j], SDL_nummodes[j], sizeof *SDL_modelist[j], cmpmodes); } } }
/* Query for the list of available video modes */ void FULLSCREEN_BuildModeList(_THIS) { _kernel_swi_regs regs; char *enumInfo = NULL; char *enum_ptr; int *blockInfo; int j; int num_modes; /* Find out how much space we need */ regs.r[0] = 2; /* Reason code */ regs.r[2] = 0; /* Number of modes to skip */ regs.r[6] = 0; /* pointer to block or 0 for count */ regs.r[7] = 0; /* Size of block in bytes */ _kernel_swi(OS_ScreenMode, ®s, ®s); num_modes = -regs.r[2]; /* Video memory should be in r[5] */ this->info.video_mem = regs.r[5] / 1024; enumInfo = (unsigned char *) SDL_malloc(-regs.r[7]); if (enumInfo == NULL) { SDL_OutOfMemory(); return; } /* Read mode information into block */ regs.r[2] = 0; regs.r[6] = (int) enumInfo; regs.r[7] = -regs.r[7]; _kernel_swi(OS_ScreenMode, ®s, ®s); enum_ptr = enumInfo; for (j = 0; j < num_modes; j++) { blockInfo = (int *) enum_ptr; if ((blockInfo[1] & 255) == 1) { /* We understand this format */ switch (blockInfo[4]) { case 3: /* 8 bits per pixel */ FULLSCREEN_AddMode(this, 8, blockInfo[2], blockInfo[3]); break; case 4: /* 15 bits per pixel */ FULLSCREEN_AddMode(this, 15, blockInfo[2], blockInfo[3]); break; case 5: /* 32 bits per pixel */ FULLSCREEN_AddMode(this, 32, blockInfo[2], blockInfo[3]); break; } } enum_ptr += blockInfo[0]; } SDL_free(enumInfo); /* Sort the mode lists */ for (j = 0; j < NUM_MODELISTS; ++j) { if (SDL_nummodes[j] > 0) { SDL_qsort(SDL_modelist[j], SDL_nummodes[j], sizeof *SDL_modelist[j], cmpmodes); } } }
void LSProfiler::dumpAllocations(lua_State *L) { lua_gc(L, LUA_GCCOLLECT, 0); #ifdef LOOM_ENABLE_JIT lmLog(gProfilerLogGroup, "Please note: Profiling under JIT does not include native function calls."); lmLog(gProfilerLogGroup, "switch to the interpreted VM in order to gather native method allocation"); #endif utList<LSProfilerTypeAllocation *> allocs; utHashTable<utPointerHashKey, MethodAllocation> methodAggr; // Prepare for object allocation dump for (UTsize i = 0; i < allocations.size(); i++) { LSProfilerTypeAllocation* alloc = allocations.at(i); allocs.push_back(alloc); } lmLog(gProfilerLogGroup, ""); lmLog(gProfilerLogGroup, "Object Allocation Dump"); lmLog(gProfilerLogGroup, "----------------------"); while (allocs.size()) { int best = -1; LSProfilerTypeAllocation *bestType = NULL; for (UTsize i = 0; i < allocs.size(); i++) { LSProfilerTypeAllocation *alloc = allocs.at(i); if (alloc->alive > best) { best = alloc->alive; bestType = alloc; } } lmAssert(bestType, "couldn't get bestType"); allocs.erase(bestType); lmLog(gProfilerLogGroup, "Alive: %i (%i KiB), Total: %i (%i KiB), Type: %s", bestType->alive, bestType->memoryCurrent/1024, bestType->total, bestType->memoryTotal/1024, bestType->type->getFullName().c_str()); if (bestType->anonTotal) { lmLog(gProfilerLogGroup, " Alive: %i, Total %i (Anonymous)", bestType->anonTotal, bestType->anonCurrent); } for (UTsize j = 0; j < bestType->methodCurrent.size(); j++) { MethodBase *methodBase = (MethodBase *)bestType->methodCurrent.keyAt(j).key(); lmAssert(methodBase == (MethodBase *)bestType->methodTotal.keyAt(j).key(), "mismatched methodbase"); int *value1 = bestType->methodCurrent.get(methodBase); int *value2 = bestType->methodTotal.get(methodBase); lmAssert(value1 && value2, "bad pointer on allocation"); lmLog(gProfilerLogGroup, " Alive %i, Total %i (%s)", (*value1), (*value2), methodBase->getFullMemberName()); } } // Aggregate method allocations for (UTsize i = 0; i < allocations.size(); i++) { LSProfilerTypeAllocation* alloc = allocations.at(i); utHashTable<utPointerHashKey, int> &methodCurrent = alloc->methodCurrent; utHashTable<utPointerHashKey, int> &methodTotal = alloc->methodTotal; for (UTsize j = 0; j < methodCurrent.size(); j++) { utPointerHashKey key = methodCurrent.keyAt(j); int *current = methodCurrent.get(key); lmAssert(current != NULL, "Internal hash table error"); int *total = methodTotal.get(key); lmAssert(total != NULL, "Internal total hash table inconsistency error"); MethodAllocation *aggr = methodAggr.get(key); if (aggr == NULL) { // First seen method, insert initial values MethodAllocation ma; ma.currentCount = *current; ma.totalCount = *total; ma.currentBytes = alloc->memoryCurrent; ma.totalBytes = alloc->memoryTotal; ma.allocations.push_back(alloc); methodAggr.insert(key, ma); } else { // Method already seen, update tracked values aggr->currentCount += *current; aggr->totalCount += *total; aggr->currentBytes += alloc->memoryCurrent; aggr->totalBytes += alloc->memoryTotal; aggr->allocations.push_back(alloc); } } } utArray<int> sortByTotal; sortByTotal.resize(methodAggr.size()); for (UTsize i = 0; i < sortByTotal.size(); i++) sortByTotal[i] = i; sortMethods = &methodAggr; SDL_qsort((void*)sortByTotal.ptr(), sortByTotal.size(), sizeof(int), sortMethodsByTotalCount); sortMethods = NULL; lmLog(gProfilerLogGroup, ""); lmLog(gProfilerLogGroup, "Aggregated Method Allocation"); lmLog(gProfilerLogGroup, "----------------------------"); for (UTsize i = 0; i < methodAggr.size(); i++) { utPointerHashKey key = methodAggr.keyAt(sortByTotal[i]); MethodBase *methodBase = (MethodBase *)key.key(); lmAssert(methodBase != NULL, "Internal aggregated method base missing"); MethodAllocation &ma = *methodAggr.get(key); lmLog(gProfilerLogGroup, "Total: %i (%i KiB), Alive: %i (%i KiB), Method: %s", ma.totalCount, ma.totalBytes/1024, ma.currentCount, ma.currentBytes/1024, methodBase->getFullMemberName()); //SDL_qsort((void*)ma.allocations.ptr(), ma.allocations.size(), sizeof(LSProfilerTypeAllocation*), sortAllocsByTotal); for (UTsize j = 0; j < ma.allocations.size(); j++) { LSProfilerTypeAllocation* alloc = ma.allocations.at(j); int &methodCurrent = *alloc->methodCurrent.get(key); int &methodTotal = *alloc->methodTotal.get(key); lmLog(gProfilerLogGroup, " Total: %i / %i (%i kiB), Alive: %i / %i (%i KiB), Type: %s", methodTotal, alloc->total, alloc->memoryTotal / 1024, methodCurrent, alloc->alive, alloc->memoryCurrent / 1024, alloc->type->getFullName().c_str()); } } }
int DIB_VideoInit(_THIS, SDL_PixelFormat *vformat) { const char *env = NULL; #ifndef NO_CHANGEDISPLAYSETTINGS int i; DEVMODE settings; #endif /* Create the window */ if ( DIB_CreateWindow(this) < 0 ) { return(-1); } #if !SDL_AUDIO_DISABLED DX5_SoundFocus(SDL_Window); #endif /* Determine the screen depth */ vformat->BitsPerPixel = DIB_SussScreenDepth(); switch (vformat->BitsPerPixel) { case 15: vformat->Rmask = 0x00007c00; vformat->Gmask = 0x000003e0; vformat->Bmask = 0x0000001f; vformat->BitsPerPixel = 16; break; case 16: vformat->Rmask = 0x0000f800; vformat->Gmask = 0x000007e0; vformat->Bmask = 0x0000001f; break; case 24: case 32: /* GDI defined as 8-8-8 */ vformat->Rmask = 0x00ff0000; vformat->Gmask = 0x0000ff00; vformat->Bmask = 0x000000ff; break; default: break; } /* See if gamma is supported on this screen */ DIB_CheckGamma(this); #ifndef NO_CHANGEDISPLAYSETTINGS settings.dmSize = sizeof(DEVMODE); settings.dmDriverExtra = 0; #ifdef _WIN32_WCE settings.dmFields = DM_DISPLAYQUERYORIENTATION; this->hidden->supportRotation = ChangeDisplaySettingsEx(NULL, &settings, NULL, CDS_TEST, NULL) == DISP_CHANGE_SUCCESSFUL; #endif /* Query for the desktop resolution */ EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &SDL_desktop_mode); this->info.current_w = SDL_desktop_mode.dmPelsWidth; this->info.current_h = SDL_desktop_mode.dmPelsHeight; /* Query for the list of available video modes */ for ( i=0; EnumDisplaySettings(NULL, i, &settings); ++i ) { DIB_AddMode(this, settings.dmBitsPerPel, settings.dmPelsWidth, settings.dmPelsHeight); #ifdef _WIN32_WCE if( this->hidden->supportRotation ) DIB_AddMode(this, settings.dmBitsPerPel, settings.dmPelsHeight, settings.dmPelsWidth); #endif } /* Sort the mode lists */ for ( i=0; i<NUM_MODELISTS; ++i ) { if ( SDL_nummodes[i] > 0 ) { SDL_qsort(SDL_modelist[i], SDL_nummodes[i], sizeof *SDL_modelist[i], cmpmodes); } } #else // WinCE and fullscreen mode: // We use only vformat->BitsPerPixel that allow SDL to // emulate other bpp (8, 32) and use triple buffer, // because SDL surface conversion is much faster than the WinCE one. // Although it should be tested on devices with graphics accelerator. DIB_AddMode(this, vformat->BitsPerPixel, GetDeviceCaps(GetDC(NULL), HORZRES), GetDeviceCaps(GetDC(NULL), VERTRES)); #endif /* !NO_CHANGEDISPLAYSETTINGS */ /* Grab an identity palette if we are in a palettized mode */ if ( vformat->BitsPerPixel <= 8 ) { /* RJR: March 28, 2000 moved palette creation to "DIB_CreatePalette" */ DIB_CreatePalette(this, vformat->BitsPerPixel); } /* Fill in some window manager capabilities */ this->info.wm_available = 1; #ifdef _WIN32_WCE this->hidden->origRotation = -1; #endif /* Allow environment override of screensaver disable. */ env = SDL_getenv("SDL_VIDEO_ALLOW_SCREENSAVER"); this->hidden->allow_screensaver = ( (env && SDL_atoi(env)) ? 1 : 0 ); /* We're done! */ return(0); }