const char* Sync_Audio::start( long sample_rate, int chan_count, int latency ) { stop(); write_buf = 0; write_pos = 0; read_buf = 0; long sample_latency = latency * sample_rate * chan_count / 1000; buf_count = sample_latency / buf_size; if ( buf_count < 2 ) buf_count = 2; bufs = (sample_t*) malloc( (long) buf_size * buf_count * sizeof *bufs ); if ( !bufs ) return "Out of memory"; free_sem = SDL_CreateSemaphore( buf_count - 1 ); if ( !free_sem ) return sdl_error( "Couldn't create semaphore" ); SDL_AudioSpec as; as.freq = sample_rate; as.format = AUDIO_S16SYS; as.channels = chan_count; as.silence = 0; as.samples = buf_size / chan_count; as.size = 0; as.callback = fill_buffer_; as.userdata = this; if ( SDL_OpenAudio( &as, 0 ) < 0 ) return sdl_error( "Couldn't open SDL audio" ); SDL_PauseAudio( 0 ); sound_open = 1; return 0; // success }
const char* Sound_Queue::start( long sample_rate, int chan_count ) { assert( !bufs ); // can only be initialized once write_buf = 0; write_pos = 0; read_buf = 0; bufs = new sample_t [(long) buf_size * buf_count]; if ( !bufs ) return "Out of memory"; currently_playing_ = bufs; free_sem = SDL_CreateSemaphore( buf_count - 1 ); if ( !free_sem ) return sdl_error( "Couldn't create semaphore" ); SDL_AudioSpec as; as.freq = (int)sample_rate; as.format = AUDIO_S16SYS; as.channels = chan_count; as.silence = 0; as.samples = buf_size / chan_count; as.size = 0; as.callback = fill_buffer_; as.userdata = this; if ( SDL_OpenAudio( &as, NULL ) < 0 ) return sdl_error( "Couldn't open SDL audio" ); SDL_PauseAudio( false ); sound_open = true; return NULL; }
static bool _gut_initWindow(const char *title, unsigned width, unsigned height, unsigned sdlflags, SDL_Window **window, SDL_GLContext *context) { if (!(gut.core->flags & CTL_SDL_INIT)) error("GUT not initialised"); SDL_GLContext old = SDL_GL_GetCurrentContext(); *window = SDL_CreateWindow( title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, sdlflags ); if (!*window) sdl_error("window could not be created"); if (old) { SDL_GL_MakeCurrent(*window, old); *context = old; } else { context = SDL_GL_CreateContext(*window); if (!context) sdl_error("context could not be created"); } if (gut.core->window.icon) SDL_SetWindowIcon(*window, gut.core->window.icon); if (!(gut.window.flags & GUT_NO_VSYNC)) { wrap_sdl_call(0,==,"vsync not available", GL_SetSwapInterval, 1); }
void draw() { if( _error ) { throw sdl_error(); } texture_ptr_t texture = texture_ptr_t( SDL_CreateTextureFromSurface( _renderer.get() , _surface.get() ) , SDL_DestroyTexture ); if( texture == NULL ) { throw sdl_error(); } SDL_RenderCopy( _renderer.get() , texture.get() , NULL , NULL ); SDL_RenderPresent( _renderer.get() ); boost::this_thread::sleep( boost::posix_time::milliseconds( 2000 )); }
/* * _update_screen() */ static void _update_screen(l_textbox *tb) { if (SDL_BlitSurface_r(tb->s, NULL, tb->screen, tb->rect)) sdl_error(tb->errorState); if (SDL_UpdateTexture( tb->texture, NULL, tb->screen->pixels, tb->screen->pitch )) sdl_error(tb->errorState); if (SDL_RenderCopy(tb->renderer, tb->texture, NULL, NULL)) sdl_error(tb->errorState); SDL_RenderPresent(tb->renderer); }
/* * l_textbox_erasechar() * Erase a character in the text window. * * Args: * 1 - Text box userdata * 2 - Numerical value of character to erase * 3 - Starting x coordinate * 4 - Flag to erase the current character or the previous character * (default is current character) * * If the 4th argument is non-zero, then the width of the given character * is subtracted from the the x coordinate. This allows us to easily erase * the cursor glyph and the previous character. */ static int l_textbox_erasechar(lua_State *L) { l_textbox *tb; uint32_t c; SDL_Color bg; int w, h; int x, goback; SDL_Rect srect, prect; tb = l_checkTextbox(L, 1); c = luaL_checkint(L, 2); x = luaL_checkint(L, 3); goback = luaL_optint(L, 4, 0); font_glyph_dim(tb->font, c, &w, &h); srect.x = x - ((goback) ? w : 0); srect.y = tb->y; srect.w = w; srect.h = h; if (SDL_FillRect(tb->s, &srect, BG_INDEX)) sdl_error(L); _update_screen(tb); lua_pushnumber(L, w); return 1; }
static int l_window_create_window(lua_State *L) { const char *title; int x, y; int w, h; uint32_t flags = 0; int i; int top; SDL_Window **window; top = lua_gettop(L); title = luaL_checkstring(L, 1); x = luaL_checkint(L, 2); y = luaL_checkint(L, 3); w = luaL_checkint(L, 4); h = luaL_checkint(L, 5); for (i = 6; i < top; i++) flags |= luaL_checkint(L, i); l_window_new(L); window = l_checkWindowP(L, -1); *window = SDL_CreateWindow(title, x, y, w, h, flags); if (*window == NULL) sdl_error(L); return 1; }
/* * l_textbox_new() */ static int l_textbox_new(lua_State *L) { l_textbox *tb; tb = lua_newuserdata(L, sizeof(l_textbox)); luaL_getmetatable(L, "l_sdl_textbox"); lua_setmetatable(L, -2); tb->errorState = L; tb->renderer = l_checkRenderer(L, 1); tb->texture = l_checkTexture(L, 2); tb->screen = l_checkSurface(L, 3); tb->rect = l_checkRect(L, 4); tb->maxChar = (uint32_t)luaL_checkinteger(L, 5); tb->font = NULL; tb->x = 0; tb->y = 0; tb->ncaps = 0; tb->nchars = 0; if (tb->rect) { tb->s = SDL_CreateRGBSurface(0, tb->rect->w, tb->rect->h, 8, 0, 0, 0, 0); if (tb->s == NULL) sdl_error(L); } else { luaL_error(L, "Rectangle not specified for textbox"); } return 1; }
const char* Sound_Queue::start( long sample_rate, int chan_count ) { assert( !bufs ); // can only be initialized once write_buf = 0; write_pos = 0; read_buf = 0; #ifndef DREAMCAST bufs = new sample_t [(long) buf_size * buf_count]; #else bufs = (sample_t *)std::malloc((long) buf_size * buf_count); #endif if ( !bufs ) { log_message(LOG_ERROR, "Run out of memory starting sound queue\n"); return "Out of memory"; } currently_playing_ = bufs; free_sem = SDL_CreateSemaphore( buf_count - 1 ); if ( !free_sem ) { log_message(LOG_ERROR, "Couldn't create semaphore starting sound queue. %s\n", SDL_GetError()); return sdl_error( "Couldn't create semaphore" ); } SDL_AudioSpec as; as.freq = sample_rate; as.format = AUDIO_S16SYS; as.channels = chan_count; as.silence = 0; as.samples = buf_size / chan_count; as.size = 0; as.callback = fill_buffer_; as.userdata = this; SDL_AudioSpec as2; // if ( SDL_OpenAudio( &as, NULL ) < 0 ) if (!(this->device = SDL_OpenAudioDevice(NULL, 0, &as, &as2, SDL_AUDIO_ALLOW_ANY_CHANGE))) { log_message(LOG_ERROR, "Couldn't open SDL audio\n"); return sdl_error( "Couldn't open SDL audio" ); } SDL_PauseAudioDevice(this->device, 0); sound_open = true; return NULL; }
void window::init() { int err = 0; if (!SDL_WasInit(SDL_INIT_VIDEO)) err = SDL_Init(SDL_INIT_VIDEO); if (err != 0) throw sdl_error("Failed to init SDL video subsystem.", err); }
static void _textbox_scroll(l_textbox *tb) { SDL_Rect r; r.x = 0; r.y = font_height(tb->font) / 2; r.w = tb->s->w; r.h = tb->s->h - font_height(tb->font) / 2; if (SDL_BlitSurface(tb->s, &r, tb->s, NULL)) sdl_error(tb->errorState); r.y = r.h; r.h = font_height(tb->font) / 2; if (SDL_FillRect(tb->s, &r, BG_INDEX)) sdl_error(tb->errorState); _update_screen(tb); }
static void process_file(const char *input_file) { FILE *fp; static unsigned char module[ASAPInfo_MAX_MODULE_LENGTH]; int module_len; ASAP *asap; const ASAPInfo *info; SDL_AudioSpec desired; fp = fopen(input_file, "rb"); if (fp == NULL) fatal_error("cannot open %s", input_file); module_len = fread(module, 1, sizeof(module), fp); fclose(fp); asap = ASAP_New(); if (!ASAP_Load(asap, input_file, module, module_len)) fatal_error("%s: unsupported file", input_file); info = ASAP_GetInfo(asap); if (song < 0) song = ASAPInfo_GetDefaultSong(info); if (!ASAP_PlaySong(asap, song, -1)) fatal_error("%s: PlaySong failed", input_file); print_header("Name", ASAPInfo_GetTitle(info)); print_header("Author", ASAPInfo_GetAuthor(info)); print_header("Date", ASAPInfo_GetDate(info)); if (SDL_Init(SDL_INIT_AUDIO) != 0) sdl_error("SDL_Init"); desired.freq = ASAP_SAMPLE_RATE; desired.format = AUDIO_S16LSB; desired.channels = ASAPInfo_GetChannels(info); desired.samples = 4096; desired.callback = audio_callback; desired.userdata = asap; if (SDL_OpenAudio(&desired, NULL) != 0) sdl_error("SDL_OpenAudio"); SDL_PauseAudio(0); printf(" playing - press Enter to quit\n"); getchar(); SDL_Quit(); }
window::window(const std::string& title, int x, int y, unsigned short width, unsigned short height) { if (!SDL_WasInit(SDL_INIT_VIDEO)) throw sdl_not_init_error(); handle = SDL_CreateWindow(title.c_str(), x, y, width, height, SDL_WINDOW_OPENGL); if (!handle) throw sdl_error("Failed to create window."); windows.push_back(this); }
int set_data(t_data *data, int wx, int wy) { (void)data; data->win_width = wx; data->win_height = wy; if (SDL_Init(SDL_INIT_EVERYTHING) < 0) return (sdl_error(0)); data->window = SDL_CreateWindow("project", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, data->win_width, data->win_height, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL); if (data->window == NULL) return (sdl_error(0)); if (!(data->context = SDL_GL_CreateContext(data->window))) return (sdl_error(0)); if (!(data->wizards = list_create())) return (0); if (!(data->selected = list_create())) return (0); data->camera.x = 0; data->camera.y = 0; data->camera.zone_width = data->win_width / 20; data->camera.zone_height = data->win_height / 20; data->camera.speed = CAM_SPEED; data->camera.state = 0; data->selection.sx = 0; data->selection.sy = 0; data->selection.cx = 0; data->selection.cy = 0; data->selection.status = 0; SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_TEXTURE_2D); return (1); }
Window::Window(int width, int height, int depth){ SDL_Init( SDL_INIT_EVERYTHING ); TTF_Init(); this->width = width; this->height = height; this->depth = depth; this->flags = SDL_HWSURFACE|SDL_RESIZABLE; mainScreen = SDL_SetVideoMode(width, height, depth, flags); if (mainScreen == NULL) { throw sdl_error((std::string)"Video initialization failed: " + SDL_GetError()); } }
void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y, SDL_Rect *clip) { int w, h; if (!tex) return; SDL_QueryTexture(tex, NULL, NULL, &w, &h); SDL_Rect dst = {x, y, w, h}; if(SDL_RenderCopy(ren, tex, clip, &dst) < 0) sdl_error("render failed"); }
static int l_textbox_clear(lua_State *L) { l_textbox *tb = l_checkTextbox(L, 1); if (SDL_FillRect(tb->s, NULL, BG_INDEX) < 0) sdl_error(L); _update_screen(tb); /* Reset the cursor */ tb->x = 0; tb->y = 0; tb->ncaps = 0; tb->nchars = 0; }
CellGrid::CellGrid() { for (int x = 0; x < grid_size; x++) { for (int y = 0; y < grid_size; y++) { grid[x][y] = AIR; } } water_surface = SDL_CreateRGBSurface(SDL_SWSURFACE, block_pixel_size, block_pixel_size, /*dimensions*/ 32, 0, 0, 0, 0 /*bits per pixel, RGBA masks*/); if (water_surface == NULL) { sdl_error(); } SDL_FillRect(water_surface, NULL, CellData::color(EXPOSED_WATER)); ticks = 0; }
/* * l_textbox_clearline() * Clear the line that the cursor is on */ static int l_textbox_clearline(lua_State *L) { l_textbox *tb; SDL_Rect srect; tb = l_checkTextbox(L, 1); srect.x = 0; srect.y = tb->y; srect.w = tb->rect->w; srect.h = font_height(tb->font); if (SDL_FillRect(tb->s, &srect, BG_INDEX)) sdl_error(L); _update_screen(tb); return 0; }
static int l_init(lua_State *L) { int top, i; uint32_t flags = 0; top = lua_gettop(L); for (i = 1; i < top; i++) flags |= (uint32_t)luaL_checkinteger(L, i); if (SDL_Init(flags) < 0) sdl_error(L); freopen("CONOUT$", "wb", stdout); freopen("CONOUT$", "wb", stderr); #if 0 TTF_Init(); #endif atexit(SDL_Quit); return 0; }
void set_card(card *c, card_num num, short xpos, short ypos, bool back) { c->value=num; if (((xpos != -1) || (ypos != -1)) && (!c->position)) c->position = try_malloc(sizeof(SDL_Rect)); if (xpos != -1) c->position->x = xpos; if (ypos != -1) c->position->y = ypos; if (num == EMPTY) { c->tex = empty_card; } else if (back){ c->tex = back_card; } else { char *file = get_card_file(num); c->tex = load_image(file, renderer); } if (c->tex == NULL) sdl_error("file not found"); }
void mutex::lock() { if (SDL_mutexP(mtx) < 0) throw sdl_error("mutex lock failed"); }
SDL_Surface* rotozoom(SDL_Surface *old, SDL_Rect *clip, Uint16 newW, Uint16 newH, char rotation) { #ifndef USE_GFX /* Doesn't work #ifdef PSP if(*access_int_global(access_resize) == resize_hardware) { SDL_Rect temp; if(clip == NULL) { temp.x = 0; temp.y = 0; temp.w = old->w; temp.h = old->h; clip = &temp; } float x_ratio; float y_ratio; if(newW == 0) { if(newH == 0) { if((rotation % 4) == 0) return old; x_ratio = 1.0f; y_ratio = 1.0f; } else { x_ratio = y_ratio = ((float)newH)/old->h; newW = old->w * x_ratio; } } else if(newH == 0) { x_ratio = y_ratio = ((float)newW)/old->w; newH = old->h * y_ratio; } else if((newW == old->w) && (newH == old->h) && ((rotation % 4) == 0)) return old; else { x_ratio = ((float)newW)/old->w; y_ratio = ((float)newH)/old->h; } device scr_acc = access_device(access_screen); SDL_Surface *ret = SDL_CreateRGBSurface( SDL_SWSURFACE, newW, newH, 24, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000); if(!ret) return NULL; SDL_Surface *hw_buf = SDL_CreateRGBSurface( SDL_HWSURFACE, 256, 256, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000); SDL_Surface *hw_draw_buf = SDL_CreateRGBSurface( SDL_HWSURFACE, 256, 256, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000); if(!hw_buf || !hw_draw_buf) { if(hw_buf) SDL_FreeSurface(hw_buf); else SDL_FreeSurface(hw_draw_buf); SDL_FreeSurface(ret); return NULL; } void *display_list = memalign(16,262144); if(!display_list) { //TODO } void *draw_buf = (void*)((int)scr_acc.screen->pixels-0x4000000); sceKernelDcacheWritebackInvalidateAll(); sceGuStart(GU_DIRECT,display_list); sceGuEnable(GU_TEXTURE_2D); //sceGuDrawBuffer(GU_PSM_8888,(void*)((int)hw_buf->pixels-0x4000000),256); sceGumMatrixMode(GU_PROJECTION); sceGumLoadIdentity(); sceGumOrtho(0.0f,256.0f,256.0f,0.0f,-1.0f,1.0f); sceGumMatrixMode(GU_VIEW); sceGumLoadIdentity(); sceGumMatrixMode(GU_MODEL); sceGumLoadIdentity(); sceGuTexMode(GU_PSM_8888, 0, 0, GU_FALSE); sceGuTexFunc(GU_TFX_DECAL, GU_TCC_RGB); sceGuTexFilter(GU_LINEAR, GU_LINEAR); sceGuTexWrap(GU_CLAMP,GU_CLAMP); sceGuTexScale(x_ratio, y_ratio); sceGuTexOffset(0.0f, 0.0f); sceGuTexImage(0,256,256,512,hw_buf->pixels); sceGuFinish(); sceGuSync(0,0); int i; for(i = 0; i < clip->h; i+=256) { int j; for(j = 0; j < clip->w; j+=256) { SDL_Rect region = { .x = clip->x + j, .y = clip->y + i, .w = 256, .h = 256 }; SDL_BlitSurface(old,NULL,hw_buf,NULL); SDL_LockSurface(hw_buf); SDL_LockSurface(scr_acc.screen); sceKernelDcacheWritebackInvalidateAll(); sceGuStart(GU_DIRECT,display_list); sceGuClear(GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT); sceGumDrawArray(GU_SPRITES, GU_TEXTURE_32BITF|GU_COLOR_8888|GU_VERTEX_32BITF|GU_TRANSFORM_2D,2,NULL,rot0); sceGuFinish(); sceGuSync(0,0); region.x = j*x_ratio; region.y = i*y_ratio; region.w *= x_ratio; region.h *= y_ratio; SDL_UnlockSurface(hw_buf); SDL_UnlockSurface(scr_acc.screen); SDL_BlitSurface(scr_acc.screen,NULL,ret,NULL); flip_screen(); } } //sceGuDrawBuffer(GU_PSM_8888,draw_buf,512); SDL_FreeSurface(hw_buf); SDL_FreeSurface(hw_draw_buf); free(display_list); return ret; } #endif */ SDL_Rect src_rect; if(clip == NULL) { src_rect.x = 0; src_rect.y = 0; src_rect.w = old->w; src_rect.h = old->h; } else src_rect = *clip; if((newW == 0) && (newH != 0)) newW = (Uint16)(((float)newH/src_rect.h)*old->w); else if((newH == 0) && (newW != 0)) newH = (Uint16)(((float)newW/src_rect.w)*old->h); else if((newW == 0) && (newH == 0)) { newW = old->w; newH = old->h; } if((clip == NULL) && (newW == old->w) && (newH == old->h) && ((rotation % 4) == 0)) return old; int current_resize_method = *access_int_global(access_resize); scale_up_cpu(); if((newW/old->w >= 2) && (newH/old->h >= 2)) current_resize_method = resize_nn; SDL_Surface *new_surf = SDL_CreateRGBSurface( SDL_SWSURFACE, (rotation % 2)?newH:newW, (rotation % 2)?newW:newH, ((current_resize_method == resize_nn) || (old->format->BytesPerPixel >= 3)) ? old->format->BitsPerPixel : ((old->format->BytesPerPixel == 1) && (old->flags & SDL_SRCCOLORKEY) && (current_resize_method == resample))? 32 : 24, (old->format->BytesPerPixel == 1)?0x00FF0000:old->format->Rmask, (old->format->BytesPerPixel == 1)?0x0000FF00:old->format->Gmask, (old->format->BytesPerPixel == 1)?0x000000FF:old->format->Bmask, (((old->format->BytesPerPixel == 1) && (old->flags & SDL_SRCCOLORKEY) && (current_resize_method == resample)) || (old->format->BytesPerPixel == 4))?0xFF000000:0 //Not sure why it has to be done this way, but it does... ); if(!new_surf) { scale_down_cpu(); sdl_error(); return NULL; } if((new_surf->format->BytesPerPixel == 1) && !(copy_palette(old, new_surf) == 1)) { SDL_FreeSurface(new_surf); scale_down_cpu(); sdl_error(); return NULL; } /* if(new_surf->format->BytesPerPixel == 4) SDL_SetAlpha(new_surf,SDL_SRCALPHA,SDL_ALPHA_TRANSPARENT); */ SDL_LockSurface(old); SDL_LockSurface(new_surf); Uint32 i, j; Uint8 *new_pixel, *row_start; Uint32 new_pitch = new_surf->pitch; Uint8 bpp = new_surf->format->BytesPerPixel; Uint32 new_to_next_row = new_pitch-bpp*(newW - 1); Uint32 new_to_prev_column = new_pitch*newH - bpp; int oldW = old->w; int oldH = old->h; Uint32 old_pitch = new_surf->pitch; Uint32 old_to_next_row = old_pitch-old->format->BytesPerPixel*(old->w - 1); Uint32 old_to_next_column = -old_pitch*oldH + old->format->BytesPerPixel; Uint32 old_bpp = old->format->BytesPerPixel; if(rotation % 2) { newW = new_surf->w; newH = new_surf->h; } char amount = rotation % 4; if((newW == oldW) && (newH == oldH)) { //Old dims == new dims. Just rotate. Uint8 *old_pixel = old->pixels; switch(amount) { case 1: new_pixel = access_pixel(new_surf,i,newH); for(j = newH; j--;) { for(i = newW; i--;) { copy_pixel(old_pixel,new_pixel,bpp); old_pixel = (Uint8*)old_pixel - old_pitch; new_pixel = (Uint8*)new_pixel - bpp; } old_pixel = (Uint8*)old_pixel + old_bpp; new_pixel = (Uint8*)new_pixel - new_to_next_row; } break; case 2: new_pixel = access_pixel(new_surf,newW-1,newH-1); for(i = newW; i--;) { for(j = newH; j--;) { copy_pixel(old_pixel,new_pixel,bpp); old_pixel = (Uint8*)old_pixel + old_pitch; new_pixel = (Uint8*)new_pixel - new_pitch; } new_pixel = (Uint8*)new_pixel + new_to_prev_column; old_pixel = (Uint8*)old_pixel + old_to_next_column; } break; case 3: new_pixel = new_surf->pixels; for(i = newW; i--;) { for(j = newH; j--;) { copy_pixel(old_pixel,new_pixel,bpp); old_pixel = (Uint8*)old_pixel + bpp; new_pixel = (Uint8*)new_pixel - new_pitch; } old_pixel = (Uint8*)old_pixel + old_to_next_row; new_pixel = (Uint8*)new_pixel + new_to_next_row; } break; } } else { switch(current_resize_method) { case resize_nn: { Uint32 locationX, locationY; Uint8 *old_pixel; new_pixel = new_surf->pixels; for(i = newH; i--;) { row_start = new_pixel; for(j = 0; j < newW; ++j,new_pixel+=bpp) { switch(amount) { case 0: locationX = (j<<16)/newW*(src_rect.w-1) + (src_rect.x<<16); locationY = (src_rect.h<<16) - ((i<<16)/newH*(src_rect.h-1) + (src_rect.y<<16)) - 1; break; case 1: locationX = (src_rect.w<<16) - ((i<<16)/newH*(src_rect.w-1) + (src_rect.x<<16)) - 1; locationY = (src_rect.h<<16) - ((j<<16)/newW*(src_rect.h-1) + (src_rect.y<<16)) - 1; break; case 2: locationX = (src_rect.w<<16) - ((j<<16)/newW*(src_rect.w-1) + (src_rect.x<<16)) - 1; locationY = ((i<<16)/newH*(src_rect.h-1) + (src_rect.y<<16)); break; case 3: locationX = (i<<16)/newH*(src_rect.w-1) + (src_rect.x<<16); locationY = (j<<16)/newW*(src_rect.h-1) + (src_rect.y<<16); break; } old_pixel = access_pixel(old,(Uint16)(locationX>>16),(Uint16)(locationY>>16)); copy_pixel(old_pixel,new_pixel,bpp); } new_pixel = row_start + new_pitch; } } break; case resample: { Uint32 locationX, locationY; Uint16 cornerX0, cornerX1, cornerY0, cornerY1; Uint8 *old_pixel0, *old_pixel1; SDL_Surface *buffer = NULL; if(old->format->BytesPerPixel < 3) { buffer = old; old = SDL_CreateRGBSurface( SDL_SWSURFACE, buffer->w, buffer->h, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000 ); SDL_SetAlpha(old,SDL_SRCALPHA,SDL_ALPHA_TRANSPARENT); SDL_UnlockSurface(buffer); SDL_BlitSurface(buffer,NULL,old,NULL); SDL_UpdateRect(old,0,0,0,0); SDL_LockSurface(old); } Uint8 k = new_surf->format->BytesPerPixel; Uint16 val; new_pixel = access_pixel(new_surf,newW-1,newH-1); for(i = newH; i--;) { row_start = new_pixel; for(j = newW; j--; new_pixel -= k*2-1) { switch(amount) { case 0: locationX = (j<<16)/newW*(src_rect.w-1) + (src_rect.x<<16); locationY = (i<<16)/newH*(src_rect.h-1) + (src_rect.y<<16); break; case 1: locationX = (i<<16)/newH*(src_rect.w-1) + (src_rect.x<<16); locationY = (src_rect.h<<16) - ((j<<16)/newW*(src_rect.h-1) + (src_rect.y<<16)) - 1; break; case 2: locationX = (src_rect.w<<16) - ((j<<16)/newW*(src_rect.w-1) + (src_rect.x<<16)) - 1; locationY = (src_rect.h<<16) - ((i<<16)/newH*(src_rect.h-1) + (src_rect.y<<16)) - 1; break; case 3: locationX = (src_rect.w<<16) - ((i<<16)/newH*(src_rect.w-1) + (src_rect.x<<16)) - 1; locationY = (j<<16)/newW*(src_rect.h-1) + (src_rect.y<<16); break; } cornerX0 = (Uint16)(locationX>>16); cornerY0 = (Uint16)(locationY>>16); cornerX1 = cornerX0; cornerY1 = cornerY0; if(amount > 1) { if(locationX-(cornerX0<<16)) --cornerX1; } else { if(cornerX0+1<oldW) ++cornerX1; } if((amount < 3) && (amount > 0)) { if(locationY-(cornerY0<<16)) --cornerY1; } else { if(cornerY0+1<oldH) ++cornerY1; } old_pixel0 = access_pixel(old,cornerX0,cornerY0); old_pixel1 = access_pixel(old,cornerX1,cornerY1); //Basically, this averages two pixels' values and puts it //into the new image. val = ((Uint16)(*(old_pixel0))+ (Uint16)(*(old_pixel1)))>>1; *((Uint8*)new_pixel) = (Uint8)val; //Do it up to four times, and also pre-increment the //variables so that the pointer is right immediately val = ((Uint16)(*(++old_pixel0))+ (Uint16)(*(++old_pixel1)))>>1; *((Uint8*)++new_pixel) = (Uint8)val; val = ((Uint16)(*(++old_pixel0))+ (Uint16)(*(++old_pixel1)))>>1; *((Uint8*)++new_pixel) = (Uint8)val; if(k == 4) { val = ((Uint16)(*(++old_pixel0))+ (Uint16)(*(++old_pixel1)))>>1; *((Uint8*)++new_pixel) = (Uint8)val; } } new_pixel = row_start - new_pitch; } if(buffer) { SDL_FreeSurface(old); old = NULL; } } break; } }
static int l_textbox_print(lua_State *L) { l_textbox *tb; btstring_t *text; const char *instring; SDL_Surface *s; SDL_Color fg; SDL_Rect srect, prect; uint32_t nchars; uint32_t index = 0; tb = l_checkTextbox(L, 1); text = bts_strcpy(luaL_checkstring(L, 2)); get_color(tb, FG_INDEX, &fg); debug("text: '%s'\n", text->buf); while (index < text->size - 1) { btstring_t *tmp; #if 0 /* * Bard's Tale doesn't use the width of the font * character's to do text wrapping. */ nchars = font_wrap(tb->font, text->buf, index, \ (tb->rect->w - tb->x)); #endif if (tb->x == 0) { while (text->buf[index] == ' ') index++; } /* * Output newlines first */ while (text->buf[index] == '\n') { index++; _textbox_newline(tb); } nchars = _textbox_wrap(tb, text, index); if (nchars) { tmp = bts_strncpy(&text->buf[index], nchars); debug("tmp = '%s'\n", tmp->buf); s = font_render(tb->font, tmp->buf, &fg); if (!s) sdl_error(L); srect.x = tb->x; srect.y = tb->y; srect.w = s->w; srect.h = s->h; if (SDL_BlitSurface_r(s, NULL, tb->s, &srect)) sdl_error(L); _update_screen(tb); tb->x += s->w; index += nchars; tb->nchars += nchars; bts_free(tmp); SDL_FreeSurface(s); } if (text->buf[index] == '\n') { while (text->buf[index] == '\n') { index++; _textbox_newline(tb); } } else if (index < text->size - 1) { /* Line wrapped. Reset x and increment y */ _textbox_newline(tb); /* Skip past any spaces at the end of the line */ while ((text->buf[index] == ' ') && (index < text->size)) index++; } } bts_free(text); return 0; }
static void set_color(l_textbox *tb, uint8_t index, SDL_Color *c) { if (SDL_SetPaletteColors(tb->s->format->palette, c, index, 1)) sdl_error(tb->errorState); }
void mutex::unlock() { if (SDL_mutexV(mtx) < 0) throw sdl_error("mutex unlock failed"); }
mutex::mutex() { mtx = SDL_CreateMutex(); if (!mtx) throw sdl_error("mutex creation failed"); }