コード例 #1
0
ファイル: sdl_mouse.c プロジェクト: muthhus/emscripten
void main_2(void* arg) {
  emscripten_run_script("window.simulateMouseEvent(10, 20, -1)"); // move from 0,0 to 10,20
  emscripten_run_script("window.simulateMouseEvent(10, 20, 0)"); // click
  emscripten_run_script("window.simulateMouseEvent(10, 20, 0)"); // click some more, but this one should be ignored through PeepEvent
  emscripten_run_script("window.simulateMouseEvent(30, 77, -1)"); // move some more
  emscripten_run_script("window.simulateMouseEvent(30, 77, 1)"); // trigger the end

  emscripten_set_main_loop(one, 0);
}
コード例 #2
0
ファイル: sdl_text.c プロジェクト: 13609594236/CrossApp
int main() {
  SDL_Init(SDL_INIT_VIDEO);
  SDL_SetVideoMode(600, 450, 32, SDL_HWSURFACE);
  SDL_StartTextInput();

  emscripten_run_script("simulateKeyEvent('a'.charCodeAt(0))"); // a
  emscripten_run_script("simulateKeyEvent('A'.charCodeAt(0))"); // A

  one();

  return 0;
}
コード例 #3
0
int main(int argc, char *argv[])
{
   SDL_Init(SDL_INIT_EVERYTHING);
   SDL_SetVideoMode(600, 400, 32, SDL_SWSURFACE);

   emscripten_run_script("keydown(37);"); // left
   result += loop1();
   emscripten_run_script("keydown(39);"); // right
   result += loop2();
   emscripten_run_script("keydown(65);"); // A
   result += alphakey();
   REPORT_RESULT(result);
   return 0;
}
コード例 #4
0
ファイル: sdl_audio.c プロジェクト: 13609594236/CrossApp
int main(int argc, char **argv) {
  SDL_Init(SDL_INIT_AUDIO);

  int ret = Mix_OpenAudio(0, 0, 0, 0); // we ignore all these..
  assert(ret == 0);

  sound = Mix_LoadWAV("sound.ogg");
  assert(sound);
  sound2 = Mix_LoadWAV("sound2.wav");
  assert(sound);

  int channel = play();
  printf( "Pausing Channel %d", channel );
  Mix_Pause(channel);
  int paused = Mix_Paused(channel);
  printf( "Channel %d %s", channel, paused ? "is paused" : "is NOT paused" );
  assert(paused);
  Mix_Resume(channel);
  paused = Mix_Paused(channel);
  printf( "Channel %d %s", channel, paused ? "is paused" : "is NOT paused" );
  assert(paused == 0);

  if (argc == 12121) play2(); // keep it alive

  emscripten_run_script("element = document.createElement('input');"
                        "element.setAttribute('type', 'button');"
                        "element.setAttribute('value', 'replay!');"
                        "element.setAttribute('onclick', 'Module[\"_play\"]()');"
                        "document.body.appendChild(element);");

  printf("you should hear two sounds. press the button to replay!\n");

  return 0;
}
コード例 #5
0
void one() {
  SDL_Event event;
  while (SDL_PollEvent(&event)) {
    switch(event.type) {
      case SDL_MOUSEMOTION: {
        SDL_MouseMotionEvent *m = (SDL_MouseMotionEvent*)&event;
        int x, y;
        SDL_GetMouseState(&x, &y);
        assert(x == m->x && y == m->y);
        printf("motion: %d,%d  %d,%d\n", m->x, m->y, m->xrel, m->yrel);
        result += 2 * (m->x + m->y + m->xrel + m->yrel);
        break;
      }
      case SDL_MOUSEBUTTONDOWN: {
        SDL_MouseButtonEvent *m = (SDL_MouseButtonEvent*)&event;
        if (m->button == 2) {
          REPORT_RESULT();
          emscripten_run_script("throw 'done'");
        }
        printf("button down: %d,%d  %d,%d\n", m->button, m->state, m->x, m->y);
        result += 3 * (m->button + m->state + m->x + m->y);
        break;
      }
      case SDL_MOUSEBUTTONUP: {
        SDL_MouseButtonEvent *m = (SDL_MouseButtonEvent*)&event;
        printf("button up: %d,%d  %d,%d\n", m->button, m->state, m->x, m->y);
        result += 5 * (m->button + m->state + m->x + m->y);
        break;
      }
    }
  }
}
コード例 #6
0
int play() {
  int channel = Mix_PlayChannel(-1, sound, 0);
  assert(channel == 0);

  emscripten_run_script("setTimeout(Module['_play2'], 500)");
  return channel;
}
コード例 #7
0
int main() {
  SDL_Init(SDL_INIT_VIDEO);
  SDL_Surface *screen = SDL_SetVideoMode(600, 450, 32, SDL_HWSURFACE);

  SDL_Rect rect = { 0, 0, 600, 450 };
  SDL_FillRect(screen, &rect, 0x2244ff00);

  emscripten_run_script("simulateMouseEvent(10, 20, -1)"); // move from 0,0 to 10,20
  emscripten_run_script("simulateMouseEvent(10, 20, 0)"); // click
  emscripten_run_script("simulateMouseEvent(30, 77, -1)"); // move some more
  emscripten_run_script("simulateMouseEvent(30, 77, 1)"); // trigger the end

  emscripten_set_main_loop(one, 0);

  return 0;
}
コード例 #8
0
void run_script_file(const std::string& file)
{
	std::ifstream ifs(file);
	std::stringstream sstr;
	sstr << ifs.rdbuf();
	std::string script = sstr.str();
	emscripten_run_script(script.c_str());
}
コード例 #9
0
int main(void)
{
  printf("hello from main page\n");

  struct sockaddr_in stSockAddr;
  int Res;
  SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

  if (-1 == SocketFD)
  {
    perror("cannot create socket");
    exit(EXIT_FAILURE);
  }

  memset(&stSockAddr, 0, sizeof(stSockAddr));

  stSockAddr.sin_family = AF_INET;
  stSockAddr.sin_port = htons(
#if EMSCRIPTEN
    3993
#else
    3992
#endif
  );
  Res = inet_pton(AF_INET, "127.0.0.1", &stSockAddr.sin_addr);

  if (0 > Res) {
    perror("error: first parameter is not a valid address family");
    close(SocketFD);
    exit(EXIT_FAILURE);
  } else if (0 == Res) {
    perror("char string (second parameter does not contain valid ipaddress)");
    close(SocketFD);
    exit(EXIT_FAILURE);
  }

  if (-1 == connect(SocketFD, (struct sockaddr *)&stSockAddr, sizeof(stSockAddr))) {
    perror("connect failed");
    close(SocketFD);
    exit(EXIT_FAILURE);

  }

#if EMSCRIPTEN
  emscripten_run_script("console.log('adding iframe');"
                        "var iframe = document.createElement('iframe');"
                        "iframe.src = 'side.html';"
                        "iframe.width = '100%';"
                        "iframe.width = '40%';"
                        "document.body.appendChild(iframe);"
                        "console.log('added.');");
  emscripten_set_main_loop(iter, 3, 0);
#else
  while (!done) iter(NULL);
#endif

  return EXIT_SUCCESS;
}
コード例 #10
0
int main(int argc, char **argv) {
  SDL_Init(SDL_INIT_AUDIO);

  int ret = Mix_OpenAudio(0, 0, 0, 0); // we ignore all these..
  assert(ret == 0);

  {
      SDL_RWops * ops = SDL_RWFromFile("sound.ogg", "r");
      sound = Mix_LoadWAV_RW(ops, 0);
      SDL_FreeRW(ops);
      assert(sound);
  }

  {
      struct stat info;
      int result = stat("noise.ogg", &info);
      char * bytes = malloc( info.st_size );
      FILE * f = fopen( "noise.ogg", "rb" );
      fread( bytes, 1, info.st_size, f  );
      fclose(f);

      SDL_RWops * ops = SDL_RWFromConstMem(bytes, info.st_size);
      sound3 = Mix_LoadWAV_RW(ops, 0);
      SDL_FreeRW(ops);
      free(bytes);
  }

  {
      music = Mix_LoadMUS("the_entertainer.ogg");
  }
  
  
  sound2 = Mix_LoadWAV("sound2.wav");
  assert(sound2);

  int channel = play();
  printf( "Pausing Channel %d", channel );
  Mix_Pause(channel);
  int paused = Mix_Paused(channel);
  printf( "Channel %d %s", channel, paused ? "is paused" : "is NOT paused" );
  assert(paused);
  Mix_Resume(channel);
  paused = Mix_Paused(channel);
  printf( "Channel %d %s", channel, paused ? "is paused" : "is NOT paused" );
  assert(paused == 0);

  if (argc == 12121) play2(); // keep it alive

  emscripten_run_script("element = document.createElement('input');"
                        "element.setAttribute('type', 'button');"
                        "element.setAttribute('value', 'replay!');"
                        "element.setAttribute('onclick', 'Module[\"_play\"]()');"
                        "document.body.appendChild(element);");

  printf("you should hear two sounds. press the button to replay!\n");

  return 0;
}
コード例 #11
0
int main (int argc, char ** argv)
{
  if (enet_initialize () != 0)
  {
    fprintf (stderr, "An error occurred while initializing ENet.\n");
    return EXIT_FAILURE;
  }
  atexit (enet_deinitialize);

  printf("creating host\n");

  host = enet_host_create (NULL /* create a client host */,
                              1 /* only allow 1 outgoing connection */,
                              2 /* allow up 2 channels to be used, 0 and 1 */,
                              57600 / 8 /* 56K modem with 56 Kbps downstream bandwidth */,
                              14400 / 8 /* 56K modem with 14 Kbps upstream bandwidth */);
  if (host == NULL)
  {
    fprintf (stderr,
              "An error occurred while trying to create an ENet client host.\n");
    exit (EXIT_FAILURE);
  }

  ENetAddress address;
  enet_address_set_host (& address, "localhost");
  address.port = SOCKK;

  printf("connecting to server...\n");

  ENetPeer *peer = enet_host_connect (host, & address, 2, 0);

  if (peer == NULL)
  {
    fprintf (stderr,
    "No available peers for initiating an ENet connection.\n");
    exit (EXIT_FAILURE);
  }

#ifdef __EMSCRIPTEN__
#if USE_IFRAME
  emscripten_run_script("console.log('adding iframe');"
                        "var iframe = document.createElement('iframe');"
                        "iframe.src = 'server.html';"
                        "iframe.width = '100%';"
                        "iframe.height = '33%';"
                        "document.body.appendChild(iframe);"
                        "console.log('added.');");
#endif
#endif

#ifdef __EMSCRIPTEN__
  emscripten_set_main_loop(main_loop, 3, 1);
#else
  while (1) main_loop();
#endif

  return 1;
}
コード例 #12
0
ファイル: sdl_key.c プロジェクト: 4ian/emscripten
int main(int argc, char **argv) {
  SDL_Init(SDL_INIT_VIDEO);
  SDL_Surface *screen = SDL_SetVideoMode(600, 450, 32, SDL_HWSURFACE);

#ifdef TEST_EMSCRIPTEN_SDL_SETEVENTHANDLER
  emscripten_SDL_SetEventHandler(EventHandler, 0);
#endif

  emscripten_set_main_loop(one, 0, 0);

  emscripten_run_script("keydown(1250);keydown(38);keyup(38);keyup(1250);"); // alt, up
  emscripten_run_script("keydown(1248);keydown(1249);keydown(40);keyup(40);keyup(1249);keyup(1248);"); // ctrl, shift, down
  emscripten_run_script("keydown(37);keyup(37);"); // left
  emscripten_run_script("keydown(39);keyup(39);"); // right

#ifdef TEST_SLEEP
  printf("sleep...\n");
  emscripten_sleep(2000);
  //emscripten_sleep_with_yield(1);
  printf("rise!\n");
#endif

  emscripten_run_script("keydown(65);keyup(65);"); // a
  emscripten_run_script("keydown(66);keyup(66);"); // b
  emscripten_run_script("keydown(100);keyup(100);"); // trigger the end

  return 0;
}
コード例 #13
0
ファイル: dukweb.c プロジェクト: alwayrun/duktape
static int dukweb__emscripten_run_script(duk_context *ctx) {
	const char *code = duk_get_string(ctx, -1);
	if (!code) {
		return DUK_RET_TYPE_ERROR;
	}
	/* FIXME: return value */
	emscripten_run_script(code);
	return 0;
}
コード例 #14
0
ファイル: main.c プロジェクト: mahiso/JudoShiai
static int handle_menu(int x1, int y1)
{
    int x, y, a, i, j;

    /* scaled coordinates 0 - 999 */
    x = x1*1000/menubg->w;
    y = y1*1000/menubg->h;
    if (Y_REG(LINE2)) {
	for (i = 0; i < NUM_TATAMIS; i++) {
	    if (X_L(tatami_x[i])) {
		conf_tatamis[i] = !conf_tatamis[i];
		configured_tatamis = 0;

		for (j = 0; j < NUM_TATAMIS; j++)
		    if (conf_tatamis[j])
			configured_tatamis++;

		for (j = 0; j < NUM_TATAMIS; j++) {
		    if (configured_tatamis) show_tatami[j] = conf_tatamis[j];
		    else show_tatami[j] = match_list[j][1].blue && match_list[j][1].white;
		}

		return TRUE;
	    }
	}
    } else if (Y_REG(LINE3)) {
	if (X_R(R1) && display_type < 2) {
	    display_type++;
	    num_lines = numlines[display_type];
	    return TRUE;
	} else if (X_L(L1) && display_type > 0) {
	    display_type--;
	    num_lines = numlines[display_type];
	    return TRUE;
	}
    } else if (Y_REG(LINE4)) {
	if (X_R(R1) || X_L(L1))
	    red_background = !red_background;
	return TRUE;
    } else if (Y_REG(LINE5)) {
	if (X_R(L1))
	    mirror_display = !mirror_display;
	return TRUE;
    } else if (Y_REG(LINE7)) {
	if (BETWEEN(X0, X0_1)) {
	    emscripten_run_script("setfullscreen()");
	    menu_on = FALSE;
	    return TRUE;
	}
    } else if ( x > 38 && x < 119 && y > 840 && y < 960) {
	menu_on = FALSE;
	return TRUE;
    }

    return FALSE;
}
コード例 #15
0
int main() {
  SDL_Init(SDL_INIT_VIDEO);
  SDL_Surface *screen = SDL_SetVideoMode(600, 450, 32, SDL_HWSURFACE);

  emscripten_set_main_loop(one, 0, 0);

  // Mimic window.close with unload event
  // (actual window.close won't work as the test will be done inside an iframe)
  emscripten_run_script("setTimeout(function() { window.dispatchEvent(new Event('unload')) }, 2000)");
}
コード例 #16
0
ファイル: storage.c プロジェクト: kidanger/Drystal
void storage_store(const char *key, const char *value)
{
	assert(key);
	assert(value);

	char *js;

	js = strjoin("localStorage['", key, "'] = '", value, "';", NULL);

	emscripten_run_script(js);
}
コード例 #17
0
ファイル: robogen_js.cpp プロジェクト: aliceconcordel/robogen
void sendJSEvent(std::string name, std::string jsonData) {

	std::string command = "self.cppEvent(\"";
	command += name;
	command += "\", ";
	command += jsonData;
	command += ")";
#ifdef EMSCRIPTEN
	emscripten_run_script(command.c_str());
#else
	std::cout << command << std::endl;
#endif
}
コード例 #18
0
ファイル: Main.cpp プロジェクト: OGRECave/ogre
int main( int argc, const char* argv[] ) {	
	try
	{
		sampleInst = new Context();
		sampleInst->initApp();
	    emscripten_set_main_loop_arg(Context::_mainLoop, sampleInst, 0, 1);
		sampleInst->closeApp();
        delete sampleInst;
        sampleInst = NULL;
	}
	catch (std::exception& e)
	{
		emscripten_run_script((std::string("alert('") + e.what() + "')").c_str());
	}
	return 0;
}
コード例 #19
0
ファイル: sdl2_mouse.c プロジェクト: WizardOfArc/emscripten
void one() {
  SDL_Event event;
  while (SDL_PollEvent(&event)) {
    switch(event.type) {
      case SDL_MOUSEMOTION: {
        SDL_MouseMotionEvent *m = (SDL_MouseMotionEvent*)&event;
        assert(m->state == 0);
        printf("motion : %d,%d  %d,%d\n", m->x, m->y, m->xrel, m->yrel);
#ifdef TEST_SDL_MOUSE_OFFSETS
        assert(eq(m->x, 5) && eq(m->y, 15) && eq(m->xrel, 5) && eq(m->yrel, 15)
          || eq(m->x, 25) && eq(m->y, 65) && eq(m->xrel, 20) && eq(m->yrel, 50));
#else        
        assert(eq(m->x, 10) && eq(m->y, 20) && eq(m->xrel, 10) && eq(m->yrel, 20)
          || eq(m->x, 30) && eq(m->y, 70) && eq(m->xrel, 20) && eq(m->yrel, 50));
#endif
        break;
      }
      case SDL_MOUSEBUTTONDOWN: {
        SDL_MouseButtonEvent *m = (SDL_MouseButtonEvent*)&event;
        if (m->button == 2) {
          REPORT_RESULT(result);
          emscripten_run_script("throw 'done'");
        }
        printf("button down : %d,%d  %d,%d\n", m->button, m->state, m->x, m->y);
#ifdef TEST_SDL_MOUSE_OFFSETS
        assert(eq(m->button, 1) && eq(m->state, 1) && eq(m->x, 5) && eq(m->y, 15));
#else
        assert(eq(m->button, 1) && eq(m->state, 1) && eq(m->x, 10) && eq(m->y, 20));
#endif
        break;
      }
      case SDL_MOUSEBUTTONUP: {
        SDL_MouseButtonEvent *m = (SDL_MouseButtonEvent*)&event;
        printf("button up : %d,%d  %d,%d\n", m->button, m->state, m->x, m->y);
#ifdef TEST_SDL_MOUSE_OFFSETS
        assert(eq(m->button, 1) && eq(m->state, 0) && eq(m->x, 5) && eq(m->y, 15));
#else
        assert(eq(m->button, 1) && eq(m->state, 0) && eq(m->x, 10) && eq(m->y, 20));
#endif
        // Remove another click we want to ignore
        assert(SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONDOWN) == 1);
        assert(SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_MOUSEBUTTONUP, SDL_MOUSEBUTTONUP) == 1);
        break;
      }
    }
  }
}
コード例 #20
0
ファイル: sdl_key.c プロジェクト: AdrienCog/emscripten
int main(int argc, char **argv) {
  SDL_Init(SDL_INIT_VIDEO);
  SDL_Surface *screen = SDL_SetVideoMode(600, 450, 32, SDL_HWSURFACE);

  emscripten_run_script("keydown(1250);keydown(38);keyup(38);keyup(1250);"); // alt, up
  emscripten_run_script("keydown(1248);keydown(1249);keydown(40);keyup(40);keyup(1249);keyup(1248);"); // ctrl, shift, down
  emscripten_run_script("keydown(37);keyup(37);"); // left
  emscripten_run_script("keydown(39);keyup(39);"); // right
  emscripten_run_script("keydown(65);keyup(65);"); // a
  emscripten_run_script("keydown(66);keyup(66);"); // b
  emscripten_run_script("keydown(100);keyup(100);"); // trigger the end

  if (argc == 1337) one(); // keep it alive

  return 0;
}
コード例 #21
0
ファイル: sdl_key.c プロジェクト: AdrienCog/emscripten
void one() {
  SDL_Event event;
  while (SDL_PollEvent(&event)) {
    switch(event.type) {
      case SDL_KEYDOWN:
        break;
      case SDL_KEYUP:
        // don't handle the modifier key events
        if (event.key.keysym.sym == SDLK_LCTRL ||
            event.key.keysym.sym == SDLK_LSHIFT ||
            event.key.keysym.sym == SDLK_LALT) {
          return;
        }
        if ((event.key.keysym.mod & KMOD_LCTRL) || (event.key.keysym.mod & KMOD_RCTRL)) {
          result *= 2;
        }
        if ((event.key.keysym.mod & KMOD_LSHIFT) || (event.key.keysym.mod & KMOD_RSHIFT)) {
          result *= 3;
        }
        if ((event.key.keysym.mod & KMOD_LALT) || (event.key.keysym.mod & KMOD_RALT)) {
          result *= 5;
        }
        switch (event.key.keysym.sym) {
          case SDLK_RIGHT: printf("right\n"); result *= 7; break;
          case SDLK_LEFT: printf("left\n"); result *= 11; break;
          case SDLK_DOWN: printf("down\n"); result *= 13; break;
          case SDLK_UP: printf("up\n"); result *= 17; break;
          case SDLK_a: printf("a\n"); result *= 19; break;
          default: {
            if (event.key.keysym.scancode == SDL_SCANCODE_B) {
              printf("b scancode\n"); result *= 23; break;
            }
            printf("unknown key: sym %d scancode %d\n", event.key.keysym.sym, event.key.keysym.scancode);
            REPORT_RESULT();
            emscripten_run_script("throw 'done'"); // comment this out to leave event handling active. Use the following to log DOM keys:
                                                   // addEventListener('keyup', function(event) { console.log(event.keyCode) }, true)
          }
        }
        break;
      default: /* Report an unhandled event */
        printf("I don't know what this event is!\n");
    }
  }
}
コード例 #22
0
ファイル: sdl_text.c プロジェクト: 13609594236/CrossApp
void one() {
  SDL_Event event;
  while (SDL_PollEvent(&event)) {
    switch (event.type) {
      case SDL_TEXTEDITING: assert(0); break;
      case SDL_TEXTINPUT:
        printf("Received %s\n", event.text.text);
        if (!strcmp("a", event.text.text)) {
          result = 1;
        } else if (!strcmp("A", event.text.text)) {
          REPORT_RESULT();
          emscripten_run_script("throw 'done'");
        }
        break;
      default: /* Report an unhandled event */
        printf("I don't know what this event is!\n");
    }
  }
}
コード例 #23
0
ファイル: JSScenario.cpp プロジェクト: aliceconcordel/robogen
boost::shared_ptr<Scenario> JSScenario::createScenario(
		boost::shared_ptr<RobogenConfig> config) {


	// super hacky -- first we generate uuid
	std::string id;
	{
		boost::uuids::uuid uuid = generator();
		std::stringstream ss;
		ss << "_myUUID_" << uuid;
		id = ss.str();
		// replace all '-' with '_' to make valid var name
		std::replace( id.begin(), id.end(), '-', '_');
	}

	//js::log("using id: ");
	//js::log(id);

	// now call the provided js with creating a new object at the end
	// and registering it with the given uuid
	{
		std::stringstream ss;
		ss << id << " = function () {\n";
		ss << "var UserScenario_" << id << " = Module.JSScenario.extend(\"JSScenario\",";
		ss << config->getScenario() << "\n";
		ss << ");\n" << "return new UserScenario_" << id << ";";
		ss << "}();";
		ss << id << ".setId('" << id << "');";
		emscripten_run_script(ss.str().c_str());
	}

	//finally use the uuid to get the pointer to the create object
	JSScenario *scenario = JSScenario::getScenario(id);
	//if (scenario == NULL) {
	//	js::log("Scenario is NULL!");
	//} else {
	//	js::log("Scenario is not NULL!");
	//}
	scenario->setRobogenConfig(config);

	return boost::shared_ptr<Scenario>(scenario);
}
コード例 #24
0
ファイル: JSScenario.cpp プロジェクト: aliceconcordel/robogen
void JSScenario::printRobotPosition() {
	osg::Vec3 pos = this->getRobot()->getCoreComponent()->getRootPosition();
	std::stringstream ss;
	ss << "console.log(\"" << pos[0] << " " <<pos[1] << " " << pos[2] << "\")";
	emscripten_run_script(ss.str().c_str());
}
コード例 #25
0
void Window::init()
{
    #ifndef EMSCRIPTEN
        Uint32 flags = 0;

        flags |= SDL_WINDOW_OPENGL;

        if(application::isFullscreen())
        {
            flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;

            // workaround for SDL bug https://bugzilla.libsdl.org/show_bug.cgi?id=3105

            SDL_DisplayMode dm;
            SDL_Event e;

            pWindow = SDL_CreateWindow("gengine", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 320, 200, SDL_WINDOW_OPENGL);
            pRenderer = SDL_CreateRenderer(
                pWindow,
                -1,
                SDL_RENDERER_ACCELERATED
                );
            context = SDL_GL_CreateContext(pWindow);

            glClear(GL_COLOR_BUFFER_BIT);
            while(SDL_PollEvent(&e));

            int display_index = SDL_GetWindowDisplayIndex(pWindow);
            SDL_GetDesktopDisplayMode(display_index, &dm);
            setExtent(dm.w, dm.h);

            SDL_GL_DeleteContext(context);
            SDL_DestroyRenderer(pRenderer);
            SDL_DestroyWindow(pWindow);
        }

        pWindow = SDL_CreateWindow(
            getName(),
            SDL_WINDOWPOS_UNDEFINED,
            SDL_WINDOWPOS_UNDEFINED,
            getWidth(),
            getHeight(),
            flags
            );

        pRenderer = SDL_CreateRenderer(
            pWindow,
            -1,
            SDL_RENDERER_ACCELERATED
            );

        context = SDL_GL_CreateContext(pWindow);
    #else
        pWindow = SDL_SetVideoMode(
            getWidth(),
            getHeight(),
            16,
            SDL_OPENGL
            );

        std::stringstream
            text;

        text << "gengineInitialize(" << application::getWidth() << ", " << application::getHeight() << ", " << application::isFullscreen() << ");";

        emscripten_run_script(text.str().c_str());
    #endif
}
コード例 #26
0
int main(int argc, char *argv[])
{
	/* Initialize SDL */
	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
		return(1);
	}

	/* Alpha blending doesn't work well at 8-bit color */
#ifdef _WIN32_WCE
	/* Pocket PC */
	w = 240;
	h = 320;
#else
	w = 640;
	h = 480;
#endif
	info = SDL_GetVideoInfo();
	if ( info->vfmt->BitsPerPixel > 8 ) {
		video_bpp = info->vfmt->BitsPerPixel;
	} else {
		video_bpp = 16;
                fprintf(stderr, "forced 16 bpp mode\n");
	}
	videoflags = SDL_SWSURFACE;
	for ( i = 1; argv[i]; ++i ) {
		if ( strcmp(argv[i], "-bpp") == 0 ) {
			video_bpp = atoi(argv[++i]);
                        if (video_bpp<=8) {
                            video_bpp=16;
                            fprintf(stderr, "forced 16 bpp mode\n");
                        }
		} else
		if ( strcmp(argv[i], "-hw") == 0 ) {
			videoflags |= SDL_HWSURFACE;
		} else
		if ( strcmp(argv[i], "-warp") == 0 ) {
			videoflags |= SDL_HWPALETTE;
		} else
		if ( strcmp(argv[i], "-width") == 0 && argv[i+1] ) {
			w = atoi(argv[++i]);
		} else
		if ( strcmp(argv[i], "-height") == 0 && argv[i+1] ) {
			h = atoi(argv[++i]);
		} else
		if ( strcmp(argv[i], "-resize") == 0 ) {
			videoflags |= SDL_RESIZABLE;
		} else
		if ( strcmp(argv[i], "-noframe") == 0 ) {
			videoflags |= SDL_NOFRAME;
		} else
		if ( strcmp(argv[i], "-fullscreen") == 0 ) {
			videoflags |= SDL_FULLSCREEN;
		} else {
			fprintf(stderr, 
			"Usage: %s [-width N] [-height N] [-bpp N] [-warp] [-hw] [-fullscreen]\n",
								argv[0]);
			quit(1);
		}
	}

	/* Set video mode */
	if ( (screen=SDL_SetVideoMode(w,h,video_bpp,videoflags)) == NULL ) {
		fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
						w, h, video_bpp, SDL_GetError());
		quit(2);
	}
	FillBackground(screen);

	/* Create the light */
	light = CreateLight(82);
	if ( light == NULL ) {
		quit(1);
	}

	/* Load the sprite */
	if ( LoadSprite(screen, "icon.bmp") < 0 ) {
		SDL_FreeSurface(light);
		quit(1);
	}

	/* Print out information about our surfaces */
	printf("Screen is at %d bits per pixel\n",screen->format->BitsPerPixel);
	if ( (screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
		printf("Screen is in video memory\n");
	} else {
		printf("Screen is in system memory\n");
	}
	if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
		printf("Screen has double-buffering enabled\n");
	}
	if ( (sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
		printf("Sprite is in video memory\n");
	} else {
		printf("Sprite is in system memory\n");
	}

	/* Run a sample blit to trigger blit acceleration */
	MoveSprite(screen, NULL);
	if ( (sprite->flags & SDL_HWACCEL) == SDL_HWACCEL ) {
		printf("Sprite blit uses hardware alpha acceleration\n");
	} else {
		printf("Sprite blit dosn't uses hardware alpha acceleration\n");
	}

	/* Set a clipping rectangle to clip the outside edge of the screen */
	{ SDL_Rect clip;
		clip.x = 32;
		clip.y = 32;
		clip.w = screen->w-(2*32);
		clip.h = screen->h-(2*32);
		SDL_SetClipRect(screen, &clip);
	}

	/* Wait for a keystroke */
	lastticks = SDL_GetTicks();
	done = 0;
	mouse_pressed = 0;
#ifndef EMSCRIPTEN
	while ( !done ) {
#else
	emscripten_run_script("report(true);");
	emscripten_set_main_loop(&main_loop, 30, 1);
}

void main_loop() {
#endif
		/* Update the frame -- move the sprite */
		if ( mouse_pressed ) {
			MoveSprite(screen, light);
			mouse_pressed = 0;
		} else {
			MoveSprite(screen, NULL);
		}

#ifndef EMSCRIPTEN
		/* Slow down the loop to 30 frames/second */
		ticks = SDL_GetTicks();
		if ( (ticks-lastticks) < FRAME_TICKS ) {
#ifdef CHECK_SLEEP_GRANULARITY
fprintf(stderr, "Sleeping %d ticks\n", FRAME_TICKS-(ticks-lastticks));
#endif
			SDL_Delay(FRAME_TICKS-(ticks-lastticks));
#ifdef CHECK_SLEEP_GRANULARITY
fprintf(stderr, "Slept %d ticks\n", (SDL_GetTicks()-ticks));
#endif
		}
		lastticks = ticks;
#endif

		/* Check for events */
		while ( SDL_PollEvent(&event) ) {
			switch (event.type) {
				case SDL_VIDEORESIZE:
					screen = SDL_SetVideoMode(event.resize.w, event.resize.h, video_bpp, videoflags);
					if ( screen ) {
						FillBackground(screen);
					}
					break;
				/* Attract sprite while mouse is held down */
				case SDL_MOUSEMOTION:
					if (event.motion.state != 0) {
						AttractSprite(event.motion.x,
								event.motion.y);
						mouse_pressed = 1;
					}
					break;
				case SDL_MOUSEBUTTONDOWN:
					if ( event.button.button == 1 ) {
						AttractSprite(event.button.x,
						              event.button.y);
						mouse_pressed = 1;
					} else {
						SDL_Rect area;

						area.x = event.button.x-16;
						area.y = event.button.y-16;
						area.w = 32;
						area.h = 32;
						SDL_FillRect(screen, &area, 0);
						SDL_UpdateRects(screen,1,&area);
					}
					break;
				case SDL_KEYDOWN:
#ifdef _WIN32_WCE
					// there is no ESC key at all
					done = 1;
#else
					if ( event.key.keysym.sym == SDLK_ESCAPE ) {
						done = 1;
					} else if (event.key.keysym.sym == SDLK_t) {
						videoflags ^= SDL_FULLSCREEN;
						screen = SDL_SetVideoMode(w, h, video_bpp, videoflags);
						if ( screen == NULL ) {
							fprintf(stderr, "Couldn't toggle video mode: %s\n",
									SDL_GetError());
							quit(2);
						}
						FillBackground(screen);
					}
#endif

					break;
				case SDL_QUIT:
					done = 1;
					break;
				default:
					break;
			}
		}

#ifndef EMSCRIPTEN
	}
	
	SDL_FreeSurface(light);
	SDL_FreeSurface(sprite);
	SDL_FreeSurface(backing);

	/* Print out some timing information */
	if ( flashes > 0 ) {
		printf("%d alpha blits, ~%4.4f ms per blit\n", 
			flashes, (float)flashtime/flashes);
	}
	
	SDL_Quit();
	return(0);
#else
	if(done) {
		SDL_FreeSurface(light);
		SDL_FreeSurface(sprite);
		SDL_FreeSurface(backing);

		/* Print out some timing information */
		if ( flashes > 0 ) {
			printf("%d alpha blits, ~%4.4f ms per blit\n", 
				flashes, (float)flashtime/flashes);
		}
		
		SDL_Quit();
	}
#endif
}
コード例 #27
0
int main()
{
  printf("you should not see this text when in a worker!\n"); // this should not crash, but also should not show up anywhere if you are in a worker
  emscripten_run_script("if (typeof postMessage !== 'undefined') { postMessage('hello from worker!') }");
}
コード例 #28
0
int main(int argc, char *argv[])
{
	Uint8  video_bpp;
	Uint32 videoflags;
	Uint8 *buffer;
	int i, k, done;
	Uint16 *buffer16;
        Uint16 color;
        Uint8  gradient;
	SDL_Color palette[256];


	/* Initialize SDL */
	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
		return(1);
	}

	video_bpp = 0;
	videoflags = SDL_SWSURFACE;
	while ( argc > 1 ) {
		--argc;
		if ( strcmp(argv[argc-1], "-bpp") == 0 ) {
			video_bpp = atoi(argv[argc]);
			--argc;
		} else
		if ( strcmp(argv[argc], "-warp") == 0 ) {
			videoflags |= SDL_HWPALETTE;
		} else
		if ( strcmp(argv[argc], "-hw") == 0 ) {
			videoflags |= SDL_HWSURFACE;
		} else
		if ( strcmp(argv[argc], "-fullscreen") == 0 ) {
			videoflags |= SDL_FULLSCREEN;
		} else {
			fprintf(stderr,
			"Usage: %s [-bpp N] [-warp] [-hw] [-fullscreen]\n",
								argv[0]);
			quit(1);
		}
	}

	/* Set 640x480 video mode */
	if ( (screen=SDL_SetVideoMode(640,480,video_bpp,videoflags)) == NULL ) {
		fprintf(stderr, "Couldn't set 640x480x%d video mode: %s\n",
						video_bpp, SDL_GetError());
		quit(2);
	}

	if (video_bpp==8) {
		/* Set a gray colormap, reverse order from white to black */
		for ( i=0; i<256; ++i ) {
			palette[i].r = 255-i;
			palette[i].g = 255-i;
			palette[i].b = 255-i;
		}
		SDL_SetColors(screen, palette, 0, 256);
	}

	/* Set the surface pixels and refresh! */
	if ( SDL_LockSurface(screen) < 0 ) {
		fprintf(stderr, "Couldn't lock the display surface: %s\n",
							SDL_GetError());
		quit(2);
	}
	buffer=(Uint8 *)screen->pixels;
	if (screen->format->BytesPerPixel!=2) {
        	for ( i=0; i<screen->h; ++i ) {
        		memset(buffer,(i*255)/screen->h, screen->pitch);
        		buffer += screen->pitch;
        	}
        }
        else
        {
		for ( i=0; i<screen->h; ++i ) {
			gradient=((i*255)/screen->h);
                        color = SDL_MapRGB(screen->format, gradient, gradient, gradient);
                        buffer16=(Uint16*)buffer;
                        for (k=0; k<screen->w; k++)
                        {
                            *(buffer16+k)=color;
                        }
			buffer += screen->pitch;
		}
        }
	SDL_UnlockSurface(screen);
	SDL_UpdateRect(screen, 0, 0, 0, 0);

	/* Load the bitmap */
	bitmap = LoadXBM(screen, picture_width, picture_height,
					(Uint8 *)picture_bits);
	if ( bitmap == NULL ) {
		quit(1);
	}
	
#ifndef EMSCRIPTEN
	SDL_Event event;
	/* Wait for a keystroke */
	done = 0;
	while ( !done ) {
#else
	emscripten_set_main_loop(&main_loop, 30, 1);
	return 0;
}

void main_loop()
{
	int done = 0;
	SDL_Event event;
	
	// Report success to the test framework to make this test work without user interaction.
	emscripten_run_script("report(true);");
#endif
		/* Check for events */
		while ( SDL_PollEvent(&event) ) {
			switch (event.type) {
				case SDL_MOUSEBUTTONDOWN: {
					SDL_Rect dst;

					dst.x = event.button.x - bitmap->w/2;
					dst.y = event.button.y - bitmap->h/2;
					dst.w = bitmap->w;
					dst.h = bitmap->h;
					SDL_BlitSurface(bitmap, NULL,
								screen, &dst);
					SDL_UpdateRects(screen,1,&dst);
					printf("User clicked at %d, %d!\n", dst.x, dst.y);
					}
					break;
				case SDL_KEYDOWN:
					/* Any key press quits the app... */
					done = 1;
					break;
				case SDL_QUIT:
					done = 1;
					break;
				default:
					break;
			}
		}
#ifndef EMSCRIPTEN
	}
コード例 #29
0
ファイル: game.c プロジェクト: HanYu1983/HanWork
// utils
void callJs(char *msg){
	emscripten_run_script(msg);
}
コード例 #30
0
ファイル: sdl_joystick.c プロジェクト: Cloudef/emscripten
void main_2(void* arg) {
  // TODO: At the moment, we only support joystick support through polling.
  emscripten_run_script("window.addNewGamepad('Pad Thai', 4, 16)");
  emscripten_run_script("window.addNewGamepad('Pad Kee Mao', 0, 4)");
  // Check that the joysticks exist properly.
  assert(SDL_NumJoysticks() == 2);
  assert(!SDL_JoystickOpened(0));
  assert(!SDL_JoystickOpened(1));
  SDL_Joystick* pad1 = SDL_JoystickOpen(0);
  assert(SDL_JoystickOpened(0));
  assert(SDL_JoystickIndex(pad1) == 0);
  assert(strncmp(SDL_JoystickName(0), "Pad Thai", 9) == 0);
  assert(strncmp(SDL_JoystickName(1), "Pad Kee Mao", 12) == 0);
  assert(SDL_JoystickNumAxes(pad1) == 4);
  assert(SDL_JoystickNumButtons(pad1) == 16);

  // Button events.
  emscripten_run_script("window.simulateGamepadButtonDown(0, 1)");
  // We didn't tell SDL to automatically update this joystick's state.
  assertNoJoystickEvent();
  SDL_JoystickUpdate();
  assertJoystickEvent(0, SDL_JOYBUTTONDOWN, 1, SDL_PRESSED);
  assert(SDL_JoystickGetButton(pad1, 1) == 1);
  // Enable automatic updates.
  SDL_JoystickEventState(SDL_ENABLE);
  assert(SDL_JoystickEventState(SDL_QUERY) == SDL_ENABLE);
  emscripten_run_script("window.simulateGamepadButtonUp(0, 1)");
  assertJoystickEvent(0, SDL_JOYBUTTONUP, 1, SDL_RELEASED);
  assert(SDL_JoystickGetButton(pad1, 1) == 0);
  // No button change: Should not result in a new event.
  emscripten_run_script("window.simulateGamepadButtonUp(0, 1)");
  assertNoJoystickEvent();
  // Joystick 1 is not opened; should not result in a new event.
  emscripten_run_script("window.simulateGamepadButtonDown(1, 1)");
  assertNoJoystickEvent();

  // Joystick wiggling
  emscripten_run_script("window.simulateAxisMotion(0, 0, 1)");
  assertJoystickEvent(0, SDL_JOYAXISMOTION, 0, 32767);
  assert(SDL_JoystickGetAxis(pad1, 0) == 32767);
  emscripten_run_script("window.simulateAxisMotion(0, 0, 0)");
  assertJoystickEvent(0, SDL_JOYAXISMOTION, 0, 0);
  assert(SDL_JoystickGetAxis(pad1, 0) == 0);
  emscripten_run_script("window.simulateAxisMotion(0, 1, -1)");
  assertJoystickEvent(0, SDL_JOYAXISMOTION, 1, -32768);
  assert(SDL_JoystickGetAxis(pad1, 1) == -32768);
  emscripten_run_script("window.simulateAxisMotion(0, 1, -1)");
  // No joystick change: Should not result in a new event.
  assertNoJoystickEvent();
  // Joystick 1 is not opened; should not result in a new event.
  emscripten_run_script("window.simulateAxisMotion(1, 1, -1)");
  assertNoJoystickEvent();

  SDL_JoystickClose(pad1);
  assert(!SDL_JoystickOpened(0));

  // Joystick 0 is closed; we should not process any new gamepad events from it.
  emscripten_run_script("window.simulateGamepadButtonDown(0, 1)");
  assertNoJoystickEvent();

  // End test.
  result = 2;
  printf("Test passed!\n");
  REPORT_RESULT();
}