/* 绘制屏幕上的内容。 * 注意程序在绘图之前调用了prepare_buffer,结束前调用了display_buffer。 * prepare_buffer会准备一个空白的绘图缓冲区,display_buffer则会将缓冲区绘制到屏幕上, * draw_pixel或draw_string绘制的内容将保存在缓冲区内(暂时不会显示在屏幕上),调用 * display_buffer后才会显示。 */ void redraw_screen() { fly_t it; const char *hit, *miss; prepare_buffer(); /* 准备缓冲区 */ /* 绘制每个字符 */ for (it = characters(); it != NULL; it = it->_next) { static char buf[2]; buf[0] = it->text + 'A'; buf[1] = 0; draw_string(buf, F2int(it->x), it->y, 15); } /* 绘制命中数、miss数、最后一次按键扫描码和fps */ draw_string(itoa(last_key_code()), SCR_HEIGHT - 8, 0, 48); hit = itoa(get_hit()); draw_string(hit, 0, SCR_WIDTH - strlen(hit) * 8, 10); miss = itoa(get_miss()); draw_string(miss, SCR_HEIGHT - 8, SCR_WIDTH - strlen(miss) * 8, 12); draw_string(itoa(get_fps()), 0, 0, 14); draw_string("FPS", 0, strlen(itoa(get_fps())) * 8, 14); display_buffer(); /* 绘制缓冲区 */ }
int main(int argc, char *argv[]) { int sock; int ret; struct afp_volume volume; thisbin=argv[0]; uid=((unsigned int) geteuid()); volume.server=NULL; if (strstr(argv[0],"mount_afp")) { if (handle_mount_afp(argc,argv)<0) return -1; } else if (prepare_buffer(argc,argv)<0) return -1; if ((sock=daemon_connect()) < 0) return -1; send_command(sock,outgoing_buffer,outgoing_len); ret=read_answer(sock); return ret; }
/*! * \internal * * Write data to a file. * */ size_t file_write(file_handle* f, void const* buf, size_t count) { if (!f) throw std::invalid_argument("file_read null arg: f"); if (!buf) throw std::invalid_argument("file_read null arg: buf"); debug::tracer DT; DT << "file_write pos: " << f->pos << " count: " << count; if (count < 1) return 0; assert(f->inode); assert(f->inode->fs); // A file can never be larger than this value - 1. uint32_t const posmax = 0xFFFFFFFF; char const* ptr = (char*) buf; uint32_t have = (uint32_t) count; uint32_t wrote = 0; // We lock the file for the duration of the read. inode_guard lock(f->inode->lock); uint32_t blocksize = f->inode->fs->blocksize; assert(blocksize <= f->rw_buf.size()); while (have > 0 && f->pos < posmax) { uint32_t offset = f->pos % blocksize; uint32_t copy_count = blocksize - offset; if (copy_count > have) copy_count = have; if (copy_count > (posmax - f->pos)) copy_count = (posmax - f->pos); assert(copy_count > 0); prepare_buffer(f, f->pos, copy_count != blocksize); std::memcpy(f->rw_buf.data() + offset, ptr, copy_count); file_mark_dirty(f); have -= copy_count; wrote += copy_count; ptr += copy_count; f->pos += copy_count; } DT << "file_write wrote: " << wrote; return wrote; }
int game_main(){ init_game(); while(1){ printg("game_main\n"); // printk("hello"); clear_letter_pressed(); printg("init_game"); black_screen(); prepare_buffer(); draw_border(); display_buffer(); //system_enable_interrupt(); init_block(); printg("gamestart"); game_loop(); sleep(2); } return 0; }
pplx::task<void> basic_cloud_page_blob_ostreambuf::upload_buffer() { auto buffer = prepare_buffer(); if (buffer->is_empty()) { return pplx::task_from_result(); } auto offset = m_current_blob_offset; m_current_blob_offset += buffer->size(); auto this_pointer = std::dynamic_pointer_cast<basic_cloud_page_blob_ostreambuf>(shared_from_this()); return m_semaphore.lock_async().then([this_pointer, buffer, offset] () { if (this_pointer->m_currentException == nullptr) { try { this_pointer->m_blob->upload_pages_async(buffer->stream(), offset, buffer->content_md5(), this_pointer->m_condition, this_pointer->m_options, this_pointer->m_context).then([this_pointer] (pplx::task<void> upload_task) { std::lock_guard<async_semaphore> guard(this_pointer->m_semaphore, std::adopt_lock); try { upload_task.wait(); } catch (const std::exception&) { this_pointer->m_currentException = std::current_exception(); } }); } catch (...) { this_pointer->m_semaphore.unlock(); } } else { this_pointer->m_semaphore.unlock(); } }); }
void play(void) { //主循环 const char *text = "http://cslab.nju.edu.cn/opsystem"; static char buf[2]; int text_len = 32; int w = SCR_WIDTH / 8; int color = 0, start_pos = 0, clk = 0; for (; ; clk = (clk + 1) % 5) { // 主循环是一个死循环 int i; prepare_buffer(); // 在绘图之前,先需要准备缓冲区 if (clk == 0) { start_pos = (start_pos + 1) % w; } color = (color + 1) % 72; for (i = 0; i < text_len; i ++) { // 计算每个字符的位置 然后显示在屏幕上 int j = (i + start_pos) % w; int d = 50 * sin(2 * PI * j / w); buf[0] = text[i]; draw_string(buf, 8 * j, SCR_HEIGHT / 2 - 4 + d, 32 + color); } // 在左下角显示键盘扫描码 draw_string(itoa(last_key), 0, SCR_HEIGHT - 8, live > 0 ? 10: 7); if (live > 0) live --; i = HZ / 60; while (i) { wait_for_interrupt(); disable_interrupt(); // 关闭中断是为了防止数据竞争(data race)。 if (timers > 0) { timers --; i --; } enable_interrupt(); } display_buffer(); // 绘图结束后,调用这个函数将绘制的图像显示到屏幕上 } }
void MapView::render(Graphics& g, render_box const& pos) { prepare_buffer(); blit_buffer(g, pos); }
u_int32_t AmJbPlayout::read(u_int32_t ts, int16_t* buf, u_int32_t len) { prepare_buffer(ts, len); buffer_get(ts, buf, len); return len; }
/*! * \internal * * Read data from a file. * * If \a count is equal to 0, this function immediately returns 0. * * \note The file position is only updated on successful reads * * \param f a pointer to an open file handle * \param buf a buffer to store read bytes in * \param count the number of bytes to read * \return the number of bytes read * * \throws std::invalid_argument thrown if \a f or \a buf are null * */ size_t file_read(file_handle* f, void* buf, size_t count) { if (!f) throw std::invalid_argument("file_read null arg: f"); if (!buf) throw std::invalid_argument("file_read null arg: buf"); debug::tracer DT; DT << "file_read pos: " << f->pos << " count: " << count; if (count < 1) return 0; assert(f->inode); assert(f->inode->fs); char* ptr = (char*) buf; uint32_t want = (uint32_t) count; uint32_t got = 0; // We lock the file for the duration of the read. inode_guard lock(f->inode->lock); uint32_t blocksize = f->inode->fs->blocksize; uint32_t filesize = f->inode->data.f_size; uint32_t pos = f->pos; assert(blocksize <= f->rw_buf.size()); while (want > 0 && pos < filesize) { uint32_t offset = pos % blocksize; uint32_t copy_count = blocksize - offset; if (copy_count > want) copy_count = want; if (copy_count > (filesize - pos)) copy_count = (filesize - pos); assert(copy_count > 0); DT << "Preparing buffer for read..."; prepare_buffer(f, pos, true); #if 0 DT << "Copying " << copy_count << " bytes, from " << "pos " << pos << " which is offset " << offset; char* bbb = f->rw_buf.data(); for (uint32_t i = 0; i < copy_count; ++i) { bbb += offset; DT << "bbb " << (uint64_t) bbb; char c = *bbb; DT << "Copying byte: " << c; ptr[i] = c; } #endif std::memcpy(ptr, f->rw_buf.data() + offset, copy_count); want -= copy_count; got += copy_count; ptr += copy_count; pos += copy_count; } DT << "pos is now " << pos << " was " << f->pos; f->pos = pos; DT << "file_read got: " << got; if (1 == got) DT << " char: " << *(ptr - 1); return got; }