support p3d specifying preferred window size to desktop app

This commit is contained in:
David Rose 2011-09-01 00:09:54 +00:00
parent 2af9dcb6eb
commit d3dc3621ac
5 changed files with 72 additions and 17 deletions

View File

@ -112,8 +112,10 @@ run_embedded(streampos read_offset, int argc, char *argv[]) {
// Read out the tokens that may interest us
if (keyword == "width") {
_win_width = atoi(value.c_str());
_got_win_size = true;
} else if (keyword == "height") {
_win_height = atoi(value.c_str());
_got_win_size = true;
} else if (keyword == "log_basename") {
_log_basename = value;
} else if (keyword == "root_dir") {
@ -233,9 +235,7 @@ run_embedded(streampos read_offset, int argc, char *argv[]) {
}
// Create a plugin instance and run the program
P3D_instance *inst = create_instance
(f, true, _win_x, _win_y, _win_width, _win_height,
argv, argc, read_offset);
P3D_instance *inst = create_instance(f, true, argv, argc, read_offset);
_instances.insert(inst);
run_main_loop();

View File

@ -120,6 +120,7 @@ run_command_line(int argc, char *argv[]) {
cerr << "Invalid value for -s: " << optarg << "\n";
return 1;
}
_got_win_size = true;
break;
case 'o':
@ -248,8 +249,11 @@ run_command_line(int argc, char *argv[]) {
int num_y_spans = int(sqrt((double)num_instance_filenames));
int num_x_spans = (num_instance_filenames + num_y_spans - 1) / num_y_spans;
int inst_width = _win_width / num_x_spans;
int inst_height = _win_height / num_y_spans;
int origin_x = _win_x;
int origin_y = _win_y;
_win_width = _win_width / num_x_spans;
_win_height = _win_height / num_y_spans;
_got_win_size = true;
for (int yi = 0; yi < num_y_spans; ++yi) {
for (int xi = 0; xi < num_x_spans; ++xi) {
@ -259,12 +263,11 @@ run_command_line(int argc, char *argv[]) {
}
// Create instance i at window slot (xi, yi).
int inst_x = _win_x + xi * inst_width;
int inst_y = _win_y + yi * inst_height;
_win_x = origin_x + xi * _win_width;
_win_y = origin_y + yi * _win_height;
P3D_instance *inst = create_instance
(instance_filenames[i], true,
inst_x, inst_y, inst_width, inst_height,
instance_args, num_instance_args);
_instances.insert(inst);
}
@ -275,7 +278,6 @@ run_command_line(int argc, char *argv[]) {
for (int i = 0; i < num_instance_filenames; ++i) {
P3D_instance *inst = create_instance
(instance_filenames[i], true,
_win_x, _win_y, _win_width, _win_height,
instance_args, num_instance_args);
_instances.insert(inst);
}

View File

@ -24,6 +24,8 @@
#include "find_root_dir.h"
#include "load_plugin.h"
#include "executionEnvironment.h"
#include "multifile.h"
#include "tinyxml.h"
// We can include this header file to get the DTOOL_PLATFORM
// definition, even though we don't link with dtool.
@ -62,6 +64,7 @@ Panda3DBase(bool console_environment) {
_win_y = -1;
_win_width = 640;
_win_height = 480;
_got_win_size = false;
_exit_with_last_instance = true;
_host_url = PANDA_PACKAGE_HOST_URL;
@ -330,7 +333,6 @@ make_parent_window() {
////////////////////////////////////////////////////////////////////
P3D_instance *Panda3DBase::
create_instance(const string &p3d, bool start_instance,
int win_x, int win_y, int win_width, int win_height,
char **args, int num_args, const int &p3d_offset) {
// Check to see if the p3d filename we were given is a URL, or a
// local file.
@ -346,6 +348,9 @@ create_instance(const string &p3d, bool start_instance,
p3d_filename.make_absolute();
os_p3d_filename = p3d_filename.to_os_specific();
}
if (is_local) {
read_p3d_info(p3d_filename);
}
// Build up the token list.
Tokens tokens = _tokens;
@ -373,7 +378,7 @@ create_instance(const string &p3d, bool start_instance,
token._value = "1";
tokens.push_back(token);
P3D_token *tokens_p;
P3D_token *tokens_p = NULL;
size_t num_tokens = tokens.size();
if (!tokens.empty()) {
tokens_p = &tokens[0];
@ -400,7 +405,7 @@ create_instance(const string &p3d, bool start_instance,
}
P3D_instance_setup_window_ptr
(inst, _window_type, win_x, win_y, win_width, win_height, &_parent_window);
(inst, _window_type, _win_x, _win_y, _win_width, _win_height, &_parent_window);
}
return inst;
@ -432,6 +437,56 @@ delete_instance(P3D_instance *inst) {
}
}
////////////////////////////////////////////////////////////////////
// Function: Panda3DBase::read_p3d_info
// Access: Protected
// Description: Opens the p3d file to read the p3d_info.xml file
// within it, looking for any locally-relevant
// parameters (like win-size).
////////////////////////////////////////////////////////////////////
bool Panda3DBase::
read_p3d_info(const Filename &p3d_filename) {
PT(Multifile) mf = new Multifile;
if (!mf->open_read(p3d_filename)) {
return false;
}
int si = mf->find_subfile("p3d_info.xml");
if (si == -1) {
return false;
}
string p3d_info;
mf->read_subfile(si, p3d_info);
istringstream strm(p3d_info);
TiXmlDocument doc;
strm >> doc;
if (strm.fail() && !strm.eof()) {
return false;
}
TiXmlElement *xpackage = doc.FirstChildElement("package");
if (xpackage == NULL) {
return false;
}
TiXmlElement *xconfig = xpackage->FirstChildElement("config");
if (xconfig == NULL) {
return false;
}
// Successfully read the p3d_info.xml file.
if (!_got_win_size) {
// If the user didn't override the size on the command line, allow
// the p3d file to request a preferred size.
if (xconfig->Attribute("width", &_win_width)) {
_got_win_size = true;
}
if (xconfig->Attribute("height", &_win_height)) {
_got_win_size = true;
}
}
return true;
}
////////////////////////////////////////////////////////////////////
// Function: Panda3DBase::parse_token
// Access: Protected

View File

@ -49,10 +49,10 @@ protected:
P3D_instance *
create_instance(const string &p3d, bool start_instance,
int win_x, int win_y, int win_width, int win_height,
char **args, int num_args, const int &p3d_offset = 0);
void delete_instance(P3D_instance *instance);
bool read_p3d_info(const Filename &p3d_filename);
bool parse_token(const char *arg);
bool parse_int_pair(const char *arg, int &x, int &y);
string lookup_token(const string &keyword) const;
@ -84,6 +84,7 @@ protected:
P3D_window_handle _parent_window;
int _win_x, _win_y;
int _win_width, _win_height;
bool _got_win_size;
bool _exit_with_last_instance;
bool _reporting_download;

View File

@ -58,10 +58,7 @@ open_p3d_file(FSRef *ref) {
}
// Create an instance.
P3D_instance *inst = create_instance
((char *)filename, true,
_win_x, _win_y, _win_width, _win_height,
NULL, 0);
create_instance((char *)filename, true, NULL, 0);
}
static pascal OSErr