Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
Quellcode |
|
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 |
int width, height, tex; unsigned char* image; int loadTexture(char* fileName) { SDL_Surface *image=IMG_Load(fileName); //SDL_DisplayFormatAlpha(image); GLuint object; glGenTextures(1,&object); glBindTexture(GL_TEXTURE_2D,object); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE); glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,image->w,image ->h,0,GL_RGBA,GL_UNSIGNED_BYTE,image->pixels); SDL_FreeSurface(image); return object; } void initor() { glClearColor(0.0,0.0,0.0,0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrthof(0.0,800,600,1.0,-1.0,1.0); glEnable(GL_BLEND); glEnable(GL_TEXTURE_2D); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); tex = loadTexture("hi.png"); } void Draw(int x, int y, Image* texture, bool blendFlag) { { //Bind Texture glBindTexture(GL_TEXTURE_2D, texture->getData()); if (blendFlag) { glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } GLfloat Vertices[] = {(float)x, (float)y, 0, (float)x + texture->getWidth(), (float)y, 0, (float)x + (float)texture->getWidth(), (float)y + (float)texture->getHeight(), 0, (float)x, (float)y + (float)texture->getHeight(), 0}; GLfloat TexCoord[] = {0, 0, 1, 0, 1, 1, 0, 1, }; GLubyte indices[] = {0,1,2, // first triangle (bottom left - top left - top right) 0,2,3}; // second triangle (bottom left - top right - bottom right) glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, 0, Vertices); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer(2, GL_FLOAT, 0, TexCoord); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); if (blendFlag) glDisable(GL_BLEND); } .... static void Cube { ... } static void Render() { Cube() Draw(10,10, ? ,blendFlag); // so? } .... // ganz viel Code und initor(); später in main |
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »Patty1991« (08.06.2020, 21:34)
Administrator
Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »Patty1991« (13.06.2020, 23:42)
Administrator
Quellcode |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
SDL_Surface *Image=IMG_Load("hi.bmp"); //typedef Image; void Draw(int x, int y,/* Image* texture */ bool blendFlag) { //Bind Texture GLuint texture= 1; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); ... |
C-/C++-Quelltext |
|
1 2 3 4 5 6 |
SDL_Surface *Image=IMG_Load("hi.bmp"); void Draw(int x, int y,Image* texture, bool blendFlag) { //Bind Texture glBindTexture(GL_TEXTURE_2D,texture->getData()); |
Administrator
C-/C++-Quelltext |
|
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
//********************************* //* SDL2 - OpenGL ES 1.1 //* Android Crossplattform Bridge for 2D and 3D //* micoAndro3D Engine alpha //*********************************** // // work in process........ // //*********************************** #include <stdlib.h> #include <stdio.h> #include <string.h> #include <math.h> #include "SDL2/SDL.h" #include "SDL_test_common.h" #include "SDL_opengles.h" #include "SDL_image.h" static SDLTest_CommonState *state; static SDL_GLContext *context = NULL; static int depth = 16; /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ static void quit(int rc) { int i; if (context != NULL) { for (i = 0; i < state->num_windows; i++) { if (context[i]) { SDL_GL_DeleteContext(context[i]); } } SDL_free(context); } SDLTest_CommonQuit(state); exit(rc); } //‐--------------------------------------------------------------------------------------------- // SDL2 Image_ loader for OpenGL ES // // use in main() - myglu=LoadTexture("example.bmp",&textw,&texth); // use in render() - draw(x,y,myglu,32,32 ) // image size //-----------------------------------‐------------------------------------------------------------- GLuint myglu; int textw,texth; bool blendFlag; GLuint textureid = 0; GLuint LoadTexture(char *filename,int *textw,int *texth) { SDL_Surface *surface; GLuint textureid; int mode; surface = IMG_Load(filename); // Or if you don't use SDL_image you can use SDL_LoadBMP here instead: // surface = SDL_LoadBMP(filename); // could not load filename if (!surface) { return 0; } // work out what format to tell glTexImage2D to use... if (surface->format->BytesPerPixel == 3) { // RGB 24bit mode = GL_RGB; } else if (surface->format->BytesPerPixel == 4) { // RGBA 32bit mode = GL_RGBA; } else { SDL_FreeSurface(surface); return 0; } *textw=surface->w; *texth=surface->h; // create one texture name glGenTextures(1, &textureid); // tell opengl to use the generated texture name glBindTexture(GL_TEXTURE_2D, textureid); // this reads from the sdl surface and puts it into an opengl texture glTexImage2D(GL_TEXTURE_2D, 0, mode, surface->w, surface->h, 0, mode, GL_UNSIGNED_BYTE, surface->pixels); // these affect how this texture is drawn later on... glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // clean up SDL_FreeSurface(surface); return textureid; } //--------------------------------- void Draw(int x, int y, GLuint textureid, int textw,int texth) { glBindTexture(GL_TEXTURE_2D, textureid); glEnable(GL_TEXTURE_2D); if (blendFlag) { glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } GLfloat Vertices[] = { (float)x, (float)y, 0, (float)x + -1, (float)y, 0, (float)x + (float)-1, (float)y + (float)-1, 0, (float)x,(float)y + (float)-1, 0}; GLfloat TexCoord[] = {0, 0, 1, 0, 1, 1, 0, 1, }; GLubyte indices[] = {0,1,2, // first triangle (bottom left - top left - top right) 0,2,3}; // second triangle (bottom left - top right - bottom right) glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, 0, Vertices); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer(2, GL_FLOAT, 0, TexCoord); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); if (blendFlag) glDisable(GL_BLEND); glDisable(GL_TEXTURE_2D ); } // ----------------------------------- // ----------------------------------- static void Cube() { // Cube // Bunte Farbe //in alle diemension static GLubyte color[8][4] = { {255, 0, 0, 0}, {255, 0, 0, 255}, {0, 255, 0, 255}, {0, 255, 0, 255}, {0, 255, 0, 255}, {255, 255, 255, 255}, {255, 0, 255, 255}, {0, 0, 255, 255} }; static GLfloat cube[8][3] = { {0.5, 0.5, -0.5}, {0.5f, -0.5f, -0.5f}, {-0.5f, -0.5f, -0.5f}, {-0.5f, 0.5f, -0.5f}, {-0.5f, 0.5f, 0.5f}, {0.5f, 0.5f, 0.5f}, {0.5f, -0.5f, 0.5f}, {-0.5f, -0.5f, 0.5f} }; //ecktpunkte zum verbinden zu alle seiten hin static GLubyte indices[36] = { 0, 3, 4, 4, 5, 0, 0, 5, 6, 6, 1, 0, 6, 7, 2, 2, 1, 6, 7, 4, 3, 3, 2, 7, 5, 4, 7, 7, 6, 5, 2, 3, 1, 3, 0, 1 }; /* Draw the cube in */ glColorPointer(4, GL_UNSIGNED_BYTE, 0, color); glEnableClientState(GL_COLOR_ARRAY); glVertexPointer(3, GL_FLOAT, 0, cube); glEnableClientState(GL_VERTEX_ARRAY); glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, indices); glMatrixMode(GL_MODELVIEW); } static void Render() { // Render aufraümen glClearColor(0.0, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // hier rendern // Draw(1,1,myglu,32,32); Draw(0,4,myglu,32,32); // "stelle alles da"* glMatrixMode(GL_MODELVIEW); } /// ------------------------------ // this is new 2020 stuff from code example in // c4droid. its free to iuse - well it works // okey we dont need it now, ony for inits();. // be carefully if you changeing int main(int argc, char *argv[]) { //für framework int fsaa, accel; int value; int i, done; SDL_DisplayMode mode; SDL_Event event; Uint32 then, now, frames; int status; /* Initialize parameters */ fsaa = 0; accel = 0; /* Initialize test framework */ state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); if (!state) { return 1; } for (i = 1; i < argc;) { int consumed; consumed = SDLTest_CommonArg(state, i); if (consumed == 0) { if (SDL_strcasecmp(argv[i], "--fsaa") == 0) { ++fsaa; consumed = 1; } else if (SDL_strcasecmp(argv[i], "--accel") == 0) { ++accel; consumed = 1; } else if (SDL_strcasecmp(argv[i], "--zdepth") == 0) { i++; if (!argv[i]) { consumed = -1; } else { depth = SDL_atoi(argv[i]); consumed = 1; } } else { consumed = -1; } } if (consumed < 0) { fprintf(stderr, "Usage: %s %s [--fsaa] [--accel] [--zdepth %%d]\n", argv[0], SDLTest_CommonUsage(state)); quit(1); } i += consumed; } /* Set OpenGL parameters */ state->window_flags |= SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_BORDERLESS; // We are using 888 instead of 555 due to a bug that happens on OnePlus 6 // With "Disable HW overlays" option off colors become weird // This is not fixed in the dev version of SDL2 at the time of writing (08/08/2018) state->gl_red_size = 8; state->gl_green_size = 8; state->gl_blue_size = 8; state->gl_depth_size = depth; if (fsaa) { state->gl_multisamplebuffers=1; state->gl_multisamplesamples=fsaa; } if (accel) { state->gl_accelerated=1; } if (!SDLTest_CommonInit(state)) { quit(2); } context = (void**) SDL_calloc(state->num_windows, sizeof(context)); if (context == NULL) { fprintf(stderr, "Out of memory!\n"); quit(2); } /* We want to use OpenGL ES 1, required for SDL 2.0.1 */ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1); /* Create OpenGL ES contexts */ for (i = 0; i < state->num_windows; i++) { context[i] = SDL_GL_CreateContext(state->windows[i]); if (!context[i]) { fprintf(stderr, "SDL_GL_CreateContext(): %s\n", SDL_GetError()); quit(2); } } if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) { SDL_GL_SetSwapInterval(1); } else { SDL_GL_SetSwapInterval(0); } myglu=LoadTexture("hi.bmp",&textw,&texth); // DrawTexture(100,100,myglu,&testw,&texth); SDL_GetCurrentDisplayMode(0, &mode); SDL_BITSPERPIXEL(mode.format); status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value); status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value); status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value); status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value); if (fsaa) { status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value); status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value); } if (accel) { status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value); } /* Set rendering settings for each context */ for (i = 0; i < state->num_windows; ++i) { float aspectAdjust; status = SDL_GL_MakeCurrent(state->windows[i], context[i]); if (status) { printf("SDL_GL_MakeCurrent(): %s\n", SDL_GetError()); /* Continue for next window */ continue; } //Android workaround state->window_w=mode.w; state->window_h=mode.h; //window screen and "camera" aspectAdjust = (4.0f / 3.0f) / ((float)state->window_w / state->window_h); glViewport(0, 0, state->window_w, state->window_h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrthof(-2.0, 2.0, -2.0 * aspectAdjust, 2.0 * aspectAdjust, -20.0, 20.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); glShadeModel(GL_SMOOTH); } /* Main render loop */ frames = 0; then = SDL_GetTicks(); done = 0; while (!done) { /* Check for events */ ++frames; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_WINDOWEVENT: switch (event.window.event) { case SDL_WINDOWEVENT_RESIZED: for (i = 0; i < state->num_windows; ++i) { if (event.window.windowID == SDL_GetWindowID(state->windows[i])) { status = SDL_GL_MakeCurrent(state->windows[i], context[i]); if (status) { printf("SDL_GL_MakeCurrent(): %s\n", SDL_GetError()); break; } /* Change view port to the new window dimensions */ glViewport(0, 0, event.window.data1, event.window.data2); /* Update window content */ Render(); SDL_GL_SwapWindow(state->windows[i]); break; } } break; } } SDLTest_CommonEvent(state, &event, &done); } for (i = 0; i < state->num_windows; ++i) { status = SDL_GL_MakeCurrent(state->windows[i], context[i]); if (status) { printf("SDL_GL_MakeCurrent(): %s\n", SDL_GetError()); /* Continue for next window */ continue; } Render(); SDL_GL_SwapWindow(state->windows[i]); } } /* Print out some timing information */ now = SDL_GetTicks(); if (now > then) { printf("%2.2f frames per second\n", ((double) frames * 1000) / (now - then)); } quit(0); return 0; } |
Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von »Patty1991« (14.06.2020, 01:30)
C-/C++-Quelltext |
|
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 |
float angle = 1.0f; static void Render() { angle += 1.0f; // Render aufraümen glClearColor(0.0, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // hier rendern // Draw(1,1,myglu,32,32); Draw(0,4,myglu,32,32); // "stelle alles da"* glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //int done_render = 0; // while (!done_render) // { glPushMatrix(); glRotatef(angle, 1, 1, 0); Cube(); glPopMatrix(); // } } |
Werbeanzeige