virtualx-engine/core/event_queue.cpp

158 lines
5.1 KiB
C++
Raw Normal View History

2014-02-10 02:10:30 +01:00
/*************************************************************************/
/* event_queue.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
2014-02-10 02:10:30 +01:00
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
2014-02-10 02:10:30 +01:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "event_queue.h"
Error EventQueue::push_call(uint32_t p_instance_ID, const StringName &p_method, VARIANT_ARG_DECLARE) {
uint8_t room_needed = sizeof(Event);
int args = 0;
if (p_arg5.get_type() != Variant::NIL)
args = 5;
else if (p_arg4.get_type() != Variant::NIL)
args = 4;
else if (p_arg3.get_type() != Variant::NIL)
args = 3;
else if (p_arg2.get_type() != Variant::NIL)
args = 2;
else if (p_arg1.get_type() != Variant::NIL)
args = 1;
2016-03-09 00:00:52 +01:00
else
args = 0;
2016-03-09 00:00:52 +01:00
room_needed += sizeof(Variant) * args;
2016-03-09 00:00:52 +01:00
ERR_FAIL_COND_V((buffer_end + room_needed) >= buffer_size, ERR_OUT_OF_MEMORY);
Event *ev = memnew_placement(&event_buffer[buffer_end], Event);
ev->args = args;
ev->instance_ID = p_instance_ID;
ev->method = p_method;
2016-03-09 00:00:52 +01:00
buffer_end += sizeof(Event);
2016-03-09 00:00:52 +01:00
if (args >= 1) {
2016-03-09 00:00:52 +01:00
Variant *v = memnew_placement(&event_buffer[buffer_end], Variant);
buffer_end += sizeof(Variant);
*v = p_arg1;
2015-04-02 12:22:17 +02:00
}
if (args >= 2) {
2016-03-09 00:00:52 +01:00
Variant *v = memnew_placement(&event_buffer[buffer_end], Variant);
buffer_end += sizeof(Variant);
*v = p_arg2;
2015-04-02 12:22:17 +02:00
}
if (args >= 3) {
2016-03-09 00:00:52 +01:00
Variant *v = memnew_placement(&event_buffer[buffer_end], Variant);
buffer_end += sizeof(Variant);
*v = p_arg3;
2015-04-02 12:22:17 +02:00
}
if (args >= 4) {
2016-03-09 00:00:52 +01:00
Variant *v = memnew_placement(&event_buffer[buffer_end], Variant);
buffer_end += sizeof(Variant);
*v = p_arg4;
2015-04-02 12:22:17 +02:00
}
if (args >= 5) {
2016-03-09 00:00:52 +01:00
Variant *v = memnew_placement(&event_buffer[buffer_end], Variant);
buffer_end += sizeof(Variant);
*v = p_arg5;
2014-02-10 02:10:30 +01:00
}
2016-03-09 00:00:52 +01:00
if (buffer_end > buffer_max_used)
buffer_max_used = buffer_end;
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
return OK;
}
void EventQueue::flush_events() {
uint32_t read_pos = 0;
2016-03-09 00:00:52 +01:00
while (read_pos < buffer_end) {
2016-03-09 00:00:52 +01:00
Event *event = (Event *)&event_buffer[read_pos];
Variant *args = (Variant *)(event + 1);
2014-02-10 02:10:30 +01:00
Object *obj = ObjectDB::get_instance(event->instance_ID);
2016-03-09 00:00:52 +01:00
if (obj) {
2014-02-10 02:10:30 +01:00
// events don't expect a return value
obj->call(event->method,
(event->args >= 1) ? args[0] : Variant(),
(event->args >= 2) ? args[1] : Variant(),
(event->args >= 3) ? args[2] : Variant(),
(event->args >= 4) ? args[3] : Variant(),
(event->args >= 5) ? args[4] : Variant());
2014-02-10 02:10:30 +01:00
}
2016-03-09 00:00:52 +01:00
if (event->args >= 1) args[0].~Variant();
if (event->args >= 2) args[1].~Variant();
if (event->args >= 3) args[2].~Variant();
if (event->args >= 4) args[3].~Variant();
if (event->args >= 5) args[4].~Variant();
2014-02-10 02:10:30 +01:00
event->~Event();
2016-03-09 00:00:52 +01:00
read_pos += sizeof(Event) + sizeof(Variant) * event->args;
2014-02-10 02:10:30 +01:00
}
2016-03-09 00:00:52 +01:00
buffer_end = 0; // reset buffer
2014-02-10 02:10:30 +01:00
}
EventQueue::EventQueue(uint32_t p_buffer_size) {
buffer_end = 0;
buffer_max_used = 0;
buffer_size = p_buffer_size;
event_buffer = memnew_arr(uint8_t, buffer_size);
2014-02-10 02:10:30 +01:00
}
EventQueue::~EventQueue() {
uint32_t read_pos = 0;
2016-03-09 00:00:52 +01:00
while (read_pos < buffer_end) {
2016-03-09 00:00:52 +01:00
Event *event = (Event *)&event_buffer[read_pos];
Variant *args = (Variant *)(event + 1);
for (int i = 0; i < event->args; i++)
2014-02-10 02:10:30 +01:00
args[i].~Variant();
event->~Event();
2016-03-09 00:00:52 +01:00
read_pos += sizeof(Event) + sizeof(Variant) * event->args;
2014-02-10 02:10:30 +01:00
}
2016-03-09 00:00:52 +01:00
2014-02-10 02:10:30 +01:00
memdelete_arr(event_buffer);
event_buffer = NULL;
2014-02-10 02:10:30 +01:00
}