source: trunk/src/platform/ps3/ps3app.cpp @ 2310

Last change on this file since 2310 was 2310, checked in by sam, 8 years ago

ps3: add support for resolution selection.

  • Property svn:keywords set to Id
File size: 4.4 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://www.wtfpl.net/ for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#if defined __CELLOS_LV2__
16#   include <sys/ppu_thread.h> /* sys_ppu_thread_get_stack_information */
17#   include <sys/spu_initialize.h>
18#   include <sys/paths.h> /* SYS_HOST_ROOT */
19#   include <cell/sysmodule.h>
20#   include <PSGL/psgl.h>
21#endif
22
23#include "core.h"
24#include "lolgl.h"
25#include "ps3app.h"
26#include "ps3input.h"
27
28namespace lol
29{
30
31/*
32 * PS3 App implementation class
33 */
34
35class Ps3AppData
36{
37    friend class Ps3App;
38
39private:
40#if defined __CELLOS_LV2__
41    static void SysCallBack(uint64_t status, uint64_t param, void *data)
42    {
43        if (status == CELL_SYSUTIL_REQUEST_EXITGAME)
44            Ticker::Shutdown();
45    }
46#endif
47};
48
49/*
50 * Public Ps3App class
51 */
52
53Ps3App::Ps3App(char const *title, ivec2 res, float fps) :
54    data(new Ps3AppData())
55{
56#if defined __CELLOS_LV2__
57    sys_spu_initialize(6, 1);
58
59    /* FIXME: we should check for CELL_SYSMODULE_ERROR_UNKNOWN and others,
60     * but what could we do anyway? */
61    cellSysmoduleLoadModule(CELL_SYSMODULE_GCM_SYS);
62    cellSysmoduleLoadModule(CELL_SYSMODULE_FS);
63    cellSysmoduleLoadModule(CELL_SYSMODULE_USBD);
64    cellSysmoduleLoadModule(CELL_SYSMODULE_IO);
65
66    cellSysutilRegisterCallback(0, Ps3AppData::SysCallBack, NULL);
67
68    PSGLinitOptions psglio =
69    {
70        enable: PSGL_INIT_MAX_SPUS | PSGL_INIT_INITIALIZE_SPUS
71                 | PSGL_INIT_HOST_MEMORY_SIZE,
72        maxSPUs: 1,
73        initializeSPUs: false,
74        persistentMemorySize: 0,
75        transientMemorySize: 0,
76        errorConsole: 0,
77        fifoSize: 0,
78        hostMemorySize: 128 * 1024 * 1024,
79    };
80
81    psglInit(&psglio);
82
83#if 0
84    sys_ppu_thread_stack_t stack;
85    sys_ppu_thread_get_stack_information(&stack);
86    printf("stack starts at %p, ends at %p\n", stack.pst_addr,
87           (uint8_t *)stack.pst_addr + stack.pst_size);
88#endif
89
90    PSGLdeviceParameters psgldp =
91    {
92        enable: PSGL_DEVICE_PARAMETERS_COLOR_FORMAT
93              | PSGL_DEVICE_PARAMETERS_DEPTH_FORMAT
94              | PSGL_DEVICE_PARAMETERS_MULTISAMPLING_MODE
95              | PSGL_DEVICE_PARAMETERS_WIDTH_HEIGHT,
96        colorFormat: GL_ARGB_SCE, /* can also be GL_RGBA16F_ARB */
97        depthFormat: GL_DEPTH_COMPONENT16, /* can also be 24-bit */
98        multisamplingMode: GL_MULTISAMPLING_4X_SQUARE_ROTATED_SCE,
99        width: 720,
100        height: 480,
101    };
102
103    /* Find closest valid resolution */
104    ivec2 const valid_resolutions[8] =
105    {
106        ivec2( 720,  480),
107        ivec2( 720,  576),
108        ivec2(1280,  720),
109        ivec2(1920, 1080),
110        ivec2( 960, 1080),
111        ivec2(1280, 1080),
112        ivec2(1440, 1080),
113        ivec2(1600, 1080),
114    };
115
116    for (int i = 0; i < 8; ++i)
117    {
118        ivec2 cur(psgldp.width, psgldp.height);
119        if (sqlength(valid_resolutions[i] - res) < sqlength(cur - res))
120        {
121            psgldp.width = valid_resolutions[i].x;
122            psgldp.height = valid_resolutions[i].y;
123        }
124    }
125
126    /* Create graphics device */
127    PSGLdevice* psgl = psglCreateDeviceExtended(&psgldp);
128    if (!psgl)
129        Log::Error("could not create PSGL device; expect imminent crash\n");
130
131    GLuint w, h;
132    psglGetDeviceDimensions(psgl, &w, &h);
133    ivec2 newres(w, h);
134    Log::Debug("resolution asked %d×%d, closest valid %d×%d, final %d×%d\n",
135               res.x, res.y, psgldp.width, psgldp.height, newres.x, newres.y);
136
137    PSGLcontext *ctx = psglCreateContext();
138    psglMakeCurrent(ctx, psgl);
139    /* TODO: load our shaders when we actually ship them */
140    //psglLoadShaderLibrary("/shaders.bin");
141    psglResetCurrentContext();
142
143    /* Initialise everything */
144    Ticker::Setup(fps);
145    Video::Setup(newres);
146    Audio::Setup(2);
147
148    /* Autoreleased objects */
149    new Ps3Input();
150#endif
151}
152
153void Ps3App::ShowPointer(bool show)
154{
155    ;
156}
157
158void Ps3App::Tick()
159{
160    /* Tick the renderer, show the frame and clamp to desired framerate. */
161    Ticker::TickDraw();
162
163#if defined __CELLOS_LV2__
164    psglSwap();
165
166    /* Check if exit callback was called */
167    cellSysutilCheckCallback();
168#endif
169}
170
171Ps3App::~Ps3App()
172{
173#if defined __CELLOS_LV2__
174    glFinish();
175#endif
176    delete data;
177}
178
179} /* namespace lol */
180
Note: See TracBrowser for help on using the repository browser.