forked from AUTOMATIC1111/IntoTheBreachLua
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.cpp
164 lines (121 loc) · 4.21 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <windows.h>
#include "lua-functions.h"
#include "utils.h"
#include "sdl-utils.h"
#include <SDL.h>
#include "sdl2.h"
#include "opengl32.h"
DECLARE_HOOK_SDL(SDL_GL_SwapWindow, void, (SDL_Window * window));
DECLARE_HOOK_SDL(SDL_PollEvent, int, (SDL_Event *evt));
DECLARE_HOOK_OPENGL(glBindTexture, void, (GLenum target, GLuint texture));
DECLARE_HOOK_OPENGL(glTexCoord2f, void, (GLfloat s, GLfloat t));
DECLARE_HOOK_OPENGL(glTexImage2D, void, (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels));
DECLARE_HOOK_OPENGL(glVertexPointer, void, (GLint size, GLenum type, GLsizei stride, const GLvoid *pointer));
DECLARE_HOOK_OPENGL(glVertexAttribPointer, void, (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void * pointer));
void setupHooks() {
sdl2Table[SDL_N_SDL_GL_SwapWindow] = (FARPROC) &SDL_GL_SwapWindow;
sdl2Table[SDL_N_SDL_PollEvent] = (FARPROC) &SDL_PollEvent;
opengl32Table[OPENGL_N_glBindTexture] = (FARPROC) &glBindTexture;
opengl32Table[OPENGL_N_glTexImage2D] = (FARPROC) &glTexImage2D;
opengl32Table[OPENGL_N_glVertexPointer] = (FARPROC) &glVertexPointer;
opengl32Table[OPENGL_N_glVertexAttribPointer] = (FARPROC) &glVertexAttribPointer;
}
#define SDL_GL_SwapWindow hook_SDL_GL_SwapWindow
#define SDL_PollEvent hook_SDL_PollEvent
#define glBindTexture hook_glBindTexture
#define glTexImage2D hook_glTexImage2D
BOOL WINAPI DllMain(HINSTANCE hInst, DWORD reason, LPVOID);
#ifdef main
#undef main
#endif
int index = 0;
void bail(lua_State *L, char *msg) {
log("\nFATAL ERROR:\n %s: %s\n\n", msg, lua_tostring(L, -1));
exit(1);
}
SDL_Window *globalWindow;
GLuint texture;
void render() {
int w, h;
SDL_GL_GetDrawableSize(globalWindow, &w, &h);
GLfloat mat[16];
float r = (float) rand() / (float) RAND_MAX;
glClearColor(sinf(0.0015f * SDL_GetTicks()) / 2 + 0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glGetFloatv(GL_MODELVIEW_MATRIX, mat);
glPushMatrix();
glGetFloatv(GL_MODELVIEW_MATRIX, mat);
glLoadIdentity();
glGetFloatv(GL_MODELVIEW_MATRIX, mat);
glOrtho(0.0, w, h, 0.0, -1.0, 1.0);
glGetFloatv(GL_MODELVIEW_MATRIX, mat);
glMatrixMode(GL_MODELVIEW);
glGetFloatv(GL_MODELVIEW_MATRIX, mat);
glPushMatrix();
glLoadIdentity();
glDisable(GL_LIGHTING);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor3f(1, 1, 1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glGetFloatv(GL_MODELVIEW_MATRIX, mat);
glBegin(GL_QUADS);
glGetFloatv(GL_MODELVIEW_MATRIX, mat);
glTexCoord2f(0, 0); glVertex2f(100, 100);
glGetFloatv(GL_MODELVIEW_MATRIX, mat);
glTexCoord2f(0, 1); glVertex2f(100, 200);
glGetFloatv(GL_MODELVIEW_MATRIX, mat);
glTexCoord2f(1, 1); glVertex2f(200, 200);
glGetFloatv(GL_MODELVIEW_MATRIX, mat);
glTexCoord2f(1, 0); glVertex2f(200, 100);
glGetFloatv(GL_MODELVIEW_MATRIX, mat);
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char *argv[]) {
DllMain(NULL, DLL_PROCESS_ATTACH, NULL);
setupHooks();
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) != 0) {
SDL_Log("Failed to initialize SDL: %s", SDL_GetError());
return 1;
}
globalWindow = SDL_CreateWindow("title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1280, 720, SDL_WINDOW_OPENGL| SDL_WINDOW_RESIZABLE);
SDL_GLContext gl_context = SDL_GL_CreateContext(globalWindow);
SDL::Surface surface("test-tr.png");
texture = surface.texture();
lua_State *L = lua_open();
installAutoexec(L);
bool quitting = false;
while(!quitting) {
SDL_Event event;
while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT) {
quitting = true;
}
if(event.type == SDL_MOUSEBUTTONUP) {
SDL_ShowCursor(0);
if(luaL_loadfile(L, "test.lua"))
bail(L, "luaL_loadfile() failed");
if(lua_pcall(L, 0, 0, 0))
bail(L, "lua_pcall() failed");
SDL_ShowCursor(1);
}
}
SDL_GL_MakeCurrent(globalWindow, gl_context);
render();
SDL_GL_SwapWindow(globalWindow);
SDL_Delay(2);
}
lua_close(L);
SDL_GL_DeleteContext(gl_context);
SDL_DestroyWindow(globalWindow);
SDL_Quit();
exit(0);
}