void PRenderer::render(PRenderState *renderState) { PASSERT(m_scene->mainCamera() != P_NULL); // Create a default render pass if none exists if (m_renderPassArray.isEmpty()) { PRenderPass *renderPass = PNEW(PRenderPass("default", m_scene)); renderPass->setCamera(m_scene->mainCamera()); renderPass->queue()->addNodes(m_scene->root()); renderPass->target()->setColorClearValue(m_backgroundColor); PShadowPass *shadowPass = PNEW(PShadowPass("shadow", m_scene)); addRenderPass(shadowPass); addRenderPass(renderPass); } if (m_renderPassArray.isEmpty()) { PLOG_WARNING("There is no valid render pass existing."); return ; } for (puint32 i = 0; i < m_renderPassArray.size(); ++i) { m_renderPassArray[i]->render(renderState); } }
pbool PFrameBuffer::createFramebuffer() { // Create framebuffer object. m_framebufferObject = PNEW(PGlFramebuffer); if (!m_framebufferObject->create(m_width, m_height, m_colorBufferFormat, m_depthBufferFormat, m_stencilBufferFormat)) { PDELETE(m_framebufferObject); PLOG_ERROR("Failed to create framebuffer object %s", m_id); return false; } // Create texture if (m_framebufferObject) { PString textureName; if (m_framebufferObject->colorBuffer()) { textureName = m_id; textureName += ":color-texture"; m_colorTexture = PNEW(PTexture(textureName.c_str(), this)); } } return true; }
void pMain(int argc, char* argv[]) { PASSERT(PActivity::s_activity != P_NULL); if (PActivity::s_activity != P_NULL) { PContextProperties contextProperties; contextProperties.m_contextName = PString("background"); contextProperties.m_archiveName = PString("background.par"); contextProperties.m_windowWidth = 640; contextProperties.m_windowHeight = 480; #if defined P_WIN32 contextProperties.m_multisamples = 2; #endif PContext* context = PNEW(MyContext(contextProperties)); context->addModule(PNEW(PResourceManager(context))); context->addModule(PNEW(PSceneManager(context))); // More modules PActivity::s_activity->addContext(context); } else { PLOG_ERROR("No running activity"); } }
void PMirror::initialize() { m_reflectionCamera = PNEW(PCamera("reflection", this)); m_reflectionCamera->setFixed(true); // Framebuffer PResourceManager *resMgr = scene()->context()->module<PResourceManager>("resource-manager"); const puint32 *rect = scene()->context()->rect(); m_reflectionFB = resMgr->createFrameBuffer(P_NULL, rect[2], rect[3], P_GLTEXTURE_FORMAT_RGBA8888, P_GLTEXTURE_FORMAT_DEPTH_COMPONENT16); m_reflectionFB->retain(); // Geometry and material setGeometry(PNEW(PGeometryPlane(resMgr))); setMaterial(PNEW(PMaterial("reflection.pmt", resMgr))); material()->parameter("texture") = m_reflectionFB->colorBuffer(); material()->parameter("inv-viewport") = pVector2(1.0f / (pfloat32)rect[2], 1.0f / (pfloat32)rect[3]); material()->parameter("color") = pVector3(0.0f, 0.0f, 1.0f); material()->parameter("blend") = 0.7f; // Render pass m_reflectionPass = PNEW(PRenderPass("reflection", scene())); }
PMaterial::PMaterial(const pchar *id, const pchar *text, pbool autoRelease, PResourceManager *resourceManager) : PEntity() { PASSERT(resourceManager != P_NULL); m_resource = resourceManager->createMaterial(text, id, autoRelease); m_resource->retain(); // Clone the parameters puint32 numParameters = m_resource->numberOfMaterialParameters(); for (puint32 i = 0; i < numParameters; ++i) { PMaterialParameter *parameter = PNEW(PMaterialParameter(*m_resource->materialParameter(i))); // The uniform location of parameter in material instance is the index of the parameter in // the material resource. parameter->setUniformLocation((pint32)i); m_materialParameters.append(parameter); } m_isTransparent = false; if (s_phonyMaterialParameter == P_NULL) { s_phonyMaterialParameter = PNEW(PMaterialParameter("phony", "Phony", P_GLSHADERUNIFORM_UNKNOWN, 1)); } }
void pMain(int argc, char* argv[]) { PASSERT(PActivity::s_activity != P_NULL); if (PActivity::s_activity != P_NULL) { PContextProperties contextProperties; contextProperties.m_contextName = PString("loadscene"); contextProperties.m_archiveName = PString("loadscene.par"); #if defined P_WIN32 contextProperties.m_windowWidth = 480; contextProperties.m_windowHeight = 800; contextProperties.m_multisamples = 2; #elif defined P_ANDROID contextProperties.m_windowWidth = 0xffffffff; contextProperties.m_windowHeight = 0xffffffff; #endif PContext* context = PNEW(MyContext(contextProperties)); context->addModule(PNEW(PResourceManager(context))); context->addModule(PNEW(PSceneManager(context))); // TODO: initialize more modules here. PActivity::s_activity->addContext(context); } else { PLOG_ERROR("No running activity"); } }
PScene::PScene(const pchar *name, PContext *context) : m_context(context) { P_OBJECT_INITIALIZE_PROPERTY(PObject); m_root = PNEW(PRootNode(name, this)); m_mainCamera = P_NULL; m_renderer = PNEW(PRenderer(this)); m_shadowQuality = SHADOWQUALITY_DEFAULT; }
PEffectGlow::PEffectGlow(PScene *scene) : PAbstractEffect(scene) { PResourceManager *resourceManager = scene->context()->module<PResourceManager>("resource-manager"); m_xMaterial = PNEW(PMaterial("glowx.pmt", resourceManager)); m_xMaterial->retain(); m_yMaterial = PNEW(PMaterial("glowy.pmt", resourceManager)); m_yMaterial->retain(); const puint32 *rect = m_scene->context()->rect(); m_xMaterial->parameter("inversed-viewport") = pVector2(1.0f / pfloat32(rect[2]), 1.0f / pfloat32(rect[3])); m_yMaterial->parameter("inversed-viewport") = pVector2(1.0f / pfloat32(rect[2]), 1.0f / pfloat32(rect[3])); m_effectRect->calibrate(m_xMaterial->vertexFormat()); }
PFrameBuffer *PFrameBuffer::createDefaultFrameBuffer() { PFrameBuffer *ret = PNEW(PFrameBuffer); ret->m_available = true; ret->m_framebufferObject = PGlFramebuffer::createDefaultFramebuffer(); return ret; }
void calculate_exp(char *exp, ...) { char temp[250]; char *exp1, *p; //转换后的用户自定义表达式 char *pexp; int len; int i; va_list ap; float val; List list; float ret; pexp = exp; i = val = ret = 0; len = strlen(exp); va_start(ap, exp); PNEW(p, (len * 4)); exp1 = p; for( ; *exp && (*exp != '\0'); ) {//循环遍历表达式 if(isalpha(*exp)) { //自定义函数表达式 val = va_arg(ap, double); assert(val); memset(temp, 0, sizeof(temp)); sprintf(temp, "%.2f", val); //strcpy(exp1, temp); i = strlen(temp); strncpy(exp1, temp, i); exp1 += i; while((*exp++) != ')') ; //跳过自定义函数字符,')'作为结束标志 }else {
pbool PResourceCache::getImages(const pchar *id, PImage **out_images, puint32 *out_numImages) { PCacheObject *imageDataObject = findObject(id); if (imageDataObject == P_NULL) { PTextureCache *cache = PNEW(PTextureCache(this, id)); if (cache->getNumberOfImages() == 0) { PLOG_ERROR("Failed to load image data at %s", id); PDELETE(cache); return false; } cache->getImages(out_images, out_numImages); insertObject(cache); return true; } PTextureCache *cache = dynamic_cast<PTextureCache *>(imageDataObject); cache->getImages(out_images, out_numImages); return true; }
PBackground::PBackground(const pchar *name, PScene *scene) : PDrawable(name, scene, P_DRAWABLE_PRIORITY_DRAWLAST) { P_OBJECT_INITIALIZE_PROPERTY(PDrawable) PResourceManager *resourceManager = scene->context()->module<PResourceManager>("resource-manager"); m_texture = P_NULL; setGeometry(PNEW(PGeometryPlane(resourceManager))); setMaterial(PNEW(PMaterial("internal/background.pmt", PBACKGROUND_PMT, false, resourceManager))); m_textureInfo = pVector4(1, 1, 0, 0); m_sizeInfo = pVector4(1, 1, 0, 0); m_layout = (LAYOUT_CENTER | LAYOUT_MIDDLE); m_dirty = true; }
MyScene::MyScene(PContext *context) : PScene("my-scene", context) { PRandom random(1001); const puint32 *rect = context->rect(); for (puint32 i = 0; i < NUM_LOGOS; ++i) { m_velocities[i][0] = (random.getFloat32() - 0.5f) * (pfloat32)rect[2] * 0.001f; m_velocities[i][1] = (random.getFloat32() - 0.5f) * (pfloat32)rect[3] * 0.001f; } // -------------------------------------------------------------- // Add camera // -------------------------------------------------------------- PCamera *camera = PNEW(PCamera("camera", this)); camera->setFixed(true); camera->transform().setLookAt(0.0f, 0.0f, 10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); camera->projection().window((pfloat32)rect[2], (pfloat32)rect[3]); setMainCamera(camera); PResourceManager* resMgr = context->module<PResourceManager>("resource-manager"); // -------------------------------------------------------------- // Scene objects // -------------------------------------------------------------- PMaterial *material = PNEW(PMaterial("texture.pmt", resMgr)); material->parameter("texture") = resMgr->getTexture("texture.png"); material->setTransparent(true); PAbstractGeometry *geometry = PNEW(PGeometryPlane(resMgr)); for (puint32 i = 0; i < NUM_LOGOS; ++i) { PString name = PString::fromUint(i); m_logos[i] = PNEW(PDrawable(name.c_str(), this)); m_logos[i]->setGeometry(geometry); m_logos[i]->setMaterial(material); m_logos[i]->transform().setTranslation(pVector3(0.0f, 0.0f, 0.0f)); m_logos[i]->transform().setScaling(pVector3(64.0f, 16.0f, 1.0f)); } setBackgroundColor(pColorRGBA(1.0f, 1.0f, 1.0f, 0.0f)); }
pbool MyContext::onInitialized() { m_scene = PNEW(MyScene(this)); PSceneManager *sceneMgr = module<PSceneManager>("scene-manager"); sceneMgr->addScene(m_scene); sceneMgr->setMainScene(m_scene); return true; }
void P_APIENTRY pInitializeLogSystem() { pchar name[128]; for (puint32 i = 0; i < P_LOG_MAX_CHANNELS; ++i) { g_logChannels[i] = PNEW(PLogOutputConsole); psprintf(name, 128, "channel%02d", i); g_logChannels[i]->setName(name); } }
pbool MyContext::onInitialized() { m_scene = PNEW(MyScene(this)); PSceneManager *sceneMgr = module<PSceneManager>("scene-manager"); sceneMgr->addScene(m_scene); sceneMgr->setMainScene(m_scene); PLOG_INFO("Press key 'P' to stop/start the rotation."); return true; }
const pchar *PResourceCache::addMeshCache(const pchar *id, pfloat32 *vertices, puint32 numVertices, puint16 *indices, puint32 numIndices, const PBox &box, PGlVertexFormat *vertexFormat, pbool autoRelease) { if (id == P_NULL) { id = generateUniqueId(); } // FIXME: check the uniqueness of the user specified id in asset space. PMeshCache *cache = PNEW(PMeshCache(this, id, vertices, numVertices, indices, numIndices, box, vertexFormat, autoRelease)); insertObject(cache); return cache->id(); }
PNetworkServer::PNetworkServer(puint16 port, PNetworkManager *manager) : PNetworkNode(NETWORK_SERVER, manager) { m_data = PNEW(PNetworkServerPrivateStruct); m_data->packet = enet_packet_create(NULL, P_NETWORK_MAX_PACKET_SIZE, ENET_PACKET_FLAG_RELIABLE); m_data->packet->referenceCount = 0x0fffffff; // This packet should never be destroyed m_nextId = 1; m_port = port; listen(m_port); }
PRenderPass::PRenderPass(const pchar *name, PScene *scene) : PEntity() , m_name(name) { PASSERT(scene != P_NULL); m_scene = scene; m_camera = P_NULL; m_renderTarget = P_NULL; m_renderQueue = P_NULL; PRenderTarget *renderTarget = PNEW(PRenderTarget); renderTarget->setViewport(m_scene->context()->rect()[0], m_scene->context()->rect()[1], m_scene->context()->rect()[2], m_scene->context()->rect()[3]); setRenderTarget(renderTarget); PRenderQueue *renderQueue = PNEW(PRenderQueue); setRenderQueue(renderQueue); }
const pchar *PResourceCache::addImages(const pchar *id, PImage **images, puint32 numImages, pbool autoRelease) { if (id == P_NULL) { id = generateUniqueId(); } // FIXME: check the uniqueness of the user specified id in asset space. PTextureCache *cache = PNEW(PTextureCache(this, id, images, numImages, autoRelease)); insertObject(cache); return cache->id(); }
pbool MyContext::onInitialized() { m_scene = PNEW(MyScene(this)); PSceneManager *sceneMgr = module<PSceneManager>("scene-manager"); sceneMgr->addScene(m_scene); sceneMgr->setMainScene(m_scene); PLOG_INFO("<Key settings>"); PLOG_INFO("P - use point lighting"); PLOG_INFO("D - use directional lighting"); PLOG_INFO("S - use spot lighting"); PLOG_INFO("H - switch low and high lighting quality"); return true; }
PMeshCache *PResourceCache::getMeshCache(const pchar *id) { PCacheObject *meshCacheObject = findObject(id); if (meshCacheObject == P_NULL) { PMeshCache *cache = PNEW(PMeshCache(this, id)); if (cache->numberOfVertices() == 0) { PDELETE(cache); return P_NULL; } insertObject(cache); return cache; } return dynamic_cast<PMeshCache *>(meshCacheObject); }
pbool PFrameBuffer::restoreResource() { m_framebufferObject = PNEW(PGlFramebuffer); if (!m_framebufferObject->create(m_width, m_height, m_colorBufferFormat, m_depthBufferFormat, m_stencilBufferFormat)) { PDELETE(m_framebufferObject); PLOG_ERROR("Failed to create framebuffer object %s", m_id); return false; } m_colorTexture->restoreResource(const_cast<PGlTexture *>(m_framebufferObject->colorBuffer())); return true; }
const pchar *PResourceCache::addMaterialCache(const pchar *id, const pchar *text, pbool autoRelease) { if (id == P_NULL) { id = generateUniqueId(); } // Check the uniqueness of the user specified id in asset space. PCacheObject *obj = findObject(id); if (obj == P_NULL) { obj = PNEW(PMaterialCache(this, id, text, autoRelease)); insertObject(obj); } return obj->id(); }
PMaterialCache *PResourceCache::getMaterialCache(const pchar *id) { PCacheObject *materialCacheObject = findObject(id); if (materialCacheObject == P_NULL) { PMaterialCache *materialCache = PNEW(PMaterialCache(this, id)); if (materialCache->text() == P_NULL) { PDELETE(materialCache); return P_NULL; } insertObject(materialCache); return materialCache; } return dynamic_cast<PMaterialCache *>(materialCacheObject); }
static void* simple_calloc(size_t nmemb, size_t size){ PNEW(mutex_local); LOCK(mutex_local); static char buffer[4096]; static char* bufoff=0; if(!bufoff){ bufoff=buffer; memset(buffer, 0, sizeof(buffer)); } void *res=0; if(size*nmemb<sizeof(buffer)+(buffer-bufoff)){ res=bufoff; bufoff+=size*nmemb; }else{ error("Too much memory requested. Increase buffer size\n"); } UNLOCK(mutex_local); return res; }
PMaterial *PMaterial::unpack(PXmlElement *element, PResourceManager *resourceManager) { PASSERT(element->isValid()); PMaterial *ret; const pchar *idValue = element->attribute("id"); if (idValue == P_NULL) { PLOG_ERROR("Failed to find the id of the material in this xml node."); return P_NULL; } ret = PNEW(PMaterial(idValue, resourceManager)); // Go over all material parameters and set their values. puint32 numParameters = ret->numberOfMaterialParameters(); for (puint32 i = 0; i < numParameters; ++i) { PMaterialParameter *parameter = ret->materialParameter(i); const pchar *parameterValue = element->attribute(parameter->name().c_str()); if (parameterValue != P_NULL) { if (!parameter->unpack(parameterValue, resourceManager)) { PLOG_WARNING("Failed to unpack parameter %s in materail %s.", parameterValue, idValue); } } } // TODO: validate the matching of type of uniform variable to the type of material parameter. const pchar *transparentValue = element->attribute("transparent"); if (transparentValue != P_NULL && pstrcmp(transparentValue, "true") == 0) { ret->setTransparent(true); } return ret; }
MyScene::MyScene(PContext *context) : PScene("my-scene", context) { m_nextSprite = 0; PResourceManager *resMgr = context->module<PResourceManager>("resource-manager"); const puint32 *rect = context->rect(); PCamera *camera = PNEW(PCamera("camera", this)); camera->setFixed(true); camera->transform().setLookAt(0.0f, 10.0f, 7.0f, 0.0f, 5.0f, 0.0f, 0.0f, 1.0f, 0.0f); camera->projection().perspective(60.0f, (pfloat32)rect[2] / (pfloat32)rect[3], 0.1f, 100.0f); setMainCamera(camera); m_plane = PNEW(PDrawable("plane", this)); m_plane->setGeometry(PNEW(PGeometryPlane(resMgr))); m_plane->setMaterial(PNEW(PMaterial("color.pmt", resMgr))); m_plane->material()->parameter("color") = P_COLOR_RED; PTexture* textures[1]; textures[0] = resMgr->getTexture("texture.png"); pchar name[] = "sprite0"; for (puint32 i = 0; i < NUM_SPRITES; ++i) { name[6]++; m_sprites[i] = PNEW(PSprite(name, this)); m_sprites[i]->setAnimationDuration(4, 2000); m_sprites[i]->setTextureAtlas(textures, 1, 2, 2, true); m_sprites[i]->transform().setScaling(pVector3(0.25f, 0.25f, 1.0f)); m_sprites[i]->transform().setTranslation(pVector3(-100000.0f, -100000.0f, -100000.0f)); } pfloat32 aspect = (pfloat32)rect[2] / (pfloat32)rect[3]; PSprite2D *legend = PNEW(PSprite2D("legend", this)); legend->transform().setScaling(pVector3(aspect * 0.25f, aspect * 0.0625f, 1.0f)); textures[0] = resMgr->getTexture("legend.png"); legend->setTextureAtlas(textures, 1, 1, 1, true); legend->setAnimationDuration(1, 10000); legend->transform().setTranslation(pVector3(0, 0.4f, -1.0f)); }
int pwin32main(int argc, char* argv[]) { // Enable memory leak checks and heap validation. _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_CHECK_ALWAYS_DF | _CRTDBG_LEAK_CHECK_DF); _CrtSetBreakAlloc(-1); int exitCode = EXIT_SUCCESS; // Create the memory debugger. PMemoryDebugger::create(); PActivity* activity = PNEW(PActivity(argc, argv)); if (!activity->initialize()) { PDELETE(activity); return EXIT_FAILURE; } pMain(argc, argv); // Set console title. SetConsoleTitle(L"Console"); // Disable the close button of the console window. HWND hConsoleWindow = GetConsoleWindow(); if (hConsoleWindow != NULL) { HMENU hMenu = GetSystemMenu(hConsoleWindow, 0); if (hMenu != NULL) { DeleteMenu(hMenu, SC_CLOSE, MF_BYCOMMAND); DrawMenuBar(hConsoleWindow); } } PContext* context = activity->findContext(puint32(0)); PASSERT(context != P_NULL); if (context == P_NULL) { exitCode = EXIT_FAILURE; PDELETE(activity); return exitCode; } PWin32Window window(context); if (!window.create()) { exitCode = EXIT_FAILURE; PDELETE(activity); return exitCode; } // Initialize the context. context->setState(P_CONTEXT_STATE_UNINITIALIZED); if (!context->initialize(context->properties()->m_windowWidth, context->properties()->m_windowHeight)) { exitCode = EXIT_FAILURE; } else { if (!context->onInitialized()) { context->setState(P_CONTEXT_STATE_ERROR); } else { PLOG_DEBUG("Starting program main loop"); context->setState(P_CONTEXT_STATE_RUNNING); } if (context->state() == P_CONTEXT_STATE_ERROR) { exitCode = EXIT_FAILURE; } // The mainloop of window. window.run(); // Right before destroy the context, user might have // something to do. context->onDestroy(); } context->destroy(); // Destroy native window. window.destroy(); // Destroy the activity activity->uninitialize(); PDELETE(activity); // Destroy the memory debugger. PMemoryDebugger::destroy(); // If debugger is present, a pause is required to keep the console output // visible. Otherwise the pause is automatic. if (IsDebuggerPresent()) { system("pause"); } return exitCode; }
/*将中缀表达式转换成后缀表达式 *@parm: sexpr 中缀表达式 *return: 存储后缀表达式的队列 **/ List convert_postfix_expression(char *expr) { List queue_exp; //后缀表达式队列,也作为操作数队列 List stack_opr; //运算符栈 char temp[25]; int i; char *pstr, *p; queue_exp = stack_opr = NULL; while(*expr != '\0') { //扫描整个中缀表达式 //isoperand = FALSE; i = 0; if(isdigit(*expr) || *expr == '.') { //是操作数(实数) memset(temp, 0, sizeof(temp)); while(isdigit(*expr) || *expr == '.') { //考虑操作数是多位的情况 temp[i++] = *expr++; }/*while*/ PNEW(pstr, i); strcpy(pstr, temp); queue_exp = list_push(queue_exp, pstr); //操作数入队列 }else { //是运算符 PNEW(pstr, OPERATOR_LEN); switch(*expr) { case '(': //左括号直接压入操作符栈 *pstr = '('; stack_opr = list_push(stack_opr, pstr); break; case '+': case '-': case '*': case '/': if(list_length(stack_opr)) {//操作符优先级比较 //当前运算符,小于栈顶运算符 while(priority(*expr) <= priority(*((char *)stack_opr->ptr))) { stack_opr = list_pop(stack_opr, &p); //取出优先级高的栈顶运算符 queue_exp = list_push(queue_exp, p); //入队 if(list_length(stack_opr) == 0) break; //如果栈顶为空,退出while }/*while*/ }/*if*/ //当前运算符进栈 *pstr = *expr; stack_opr = list_push(stack_opr, pstr); break; case ')': /*取出运算符栈中左小括号以 上得全部运算符压入操作符队列中 */ while(*((char *)stack_opr->ptr) != '(') { stack_opr = list_pop(stack_opr, &p); //出栈 queue_exp = list_push(queue_exp, p); //入队 }/*while*/ stack_opr = list_pop(stack_opr, &p); //丢掉'(' free(p); break; default : printf("----------%c\n",*expr); assert(!"expression format is error\n"); break; }/*switch*/ expr++; }/*else*/ #if DEBUG print_list(queue_exp, "queue"); print_list(stack_opr, "stack"); #endif }/*while*/ //将操作符全部压入操作数队列中 while(list_length(stack_opr)) { stack_opr = list_pop(stack_opr, &p); //出栈 queue_exp = list_push(queue_exp, p); //入队 } #if DEBUG print_list(queue_exp, "queue"); print_list(stack_opr, "stack"); #endif return queue_exp; }