Skip to content

Latest commit

 

History

History
193 lines (168 loc) · 6.08 KB

showimage.c

File metadata and controls

193 lines (168 loc) · 6.08 KB
 
Aug 10, 2000
Aug 10, 2000
1
/*
Dec 31, 2011
Dec 31, 2011
2
showimage: A test application for the SDL image loading library.
Mar 1, 2018
Mar 1, 2018
3
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
Dec 31, 2011
Dec 31, 2011
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Aug 10, 2000
Aug 10, 2000
20
21
22
23
*/
#include <stdlib.h>
#include <stdio.h>
Apr 5, 2001
Apr 5, 2001
24
#include <string.h>
Aug 10, 2000
Aug 10, 2000
25
26
27
28
29
30
#include "SDL.h"
#include "SDL_image.h"
/* Draw a Gimpish background pattern to show transparency in the image */
Jan 23, 2012
Jan 23, 2012
31
static void draw_background(SDL_Renderer *renderer, int w, int h)
Aug 10, 2000
Aug 10, 2000
32
{
Jan 23, 2012
Jan 23, 2012
33
34
35
36
37
38
39
40
41
42
43
44
45
SDL_Color col[2] = {
{ 0x66, 0x66, 0x66, 0xff },
{ 0x99, 0x99, 0x99, 0xff },
};
int i, x, y;
SDL_Rect rect;
rect.w = 8;
rect.h = 8;
for (y = 0; y < h; y += rect.h) {
for (x = 0; x < w; x += rect.w) {
/* use an 8x8 checkerboard pattern */
i = (((x ^ y) >> 3) & 1);
Mar 24, 2013
Mar 24, 2013
46
SDL_SetRenderDrawColor(renderer, col[i].r, col[i].g, col[i].b, col[i].a);
Jan 23, 2012
Jan 23, 2012
47
48
49
50
51
rect.x = x;
rect.y = y;
SDL_RenderFillRect(renderer, &rect);
}
Aug 10, 2000
Aug 10, 2000
52
53
54
}
}
Sep 1, 2000
Sep 1, 2000
55
int main(int argc, char *argv[])
Aug 10, 2000
Aug 10, 2000
56
{
Jan 23, 2012
Jan 23, 2012
57
58
59
60
61
62
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *texture;
Uint32 flags;
int i, w, h, done;
SDL_Event event;
Jun 3, 2013
Jun 3, 2013
63
const char *saveFile = NULL;
Jan 23, 2012
Jan 23, 2012
64
65
66
/* Check command line usage */
if ( ! argv[1] ) {
Oct 26, 2018
Oct 26, 2018
67
SDL_Log("Usage: %s [-fullscreen] [-save file.png] <image_file> ...\n", argv[0]);
Jan 23, 2012
Jan 23, 2012
68
69
70
return(1);
}
Apr 24, 2013
Apr 24, 2013
71
flags = SDL_WINDOW_HIDDEN;
Jan 23, 2012
Jan 23, 2012
72
for ( i=1; argv[i]; ++i ) {
Nov 5, 2017
Nov 5, 2017
73
if ( SDL_strcmp(argv[i], "-fullscreen") == 0 ) {
Jan 23, 2012
Jan 23, 2012
74
75
76
77
SDL_ShowCursor(0);
flags |= SDL_WINDOW_FULLSCREEN;
}
}
May 22, 2013
May 22, 2013
78
Sep 12, 2017
Sep 12, 2017
79
if (SDL_Init(SDL_INIT_VIDEO) == -1) {
Oct 26, 2018
Oct 26, 2018
80
SDL_Log("SDL_Init(SDL_INIT_VIDEO) failed: %s\n", SDL_GetError());
Sep 12, 2017
Sep 12, 2017
81
82
83
return(2);
}
Jan 23, 2012
Jan 23, 2012
84
if (SDL_CreateWindowAndRenderer(0, 0, flags, &window, &renderer) < 0) {
Oct 26, 2018
Oct 26, 2018
85
SDL_Log("SDL_CreateWindowAndRenderer() failed: %s\n", SDL_GetError());
Jan 23, 2012
Jan 23, 2012
86
87
88
89
return(2);
}
for ( i=1; argv[i]; ++i ) {
Nov 5, 2017
Nov 5, 2017
90
if ( SDL_strcmp(argv[i], "-fullscreen") == 0 ) {
Jan 23, 2012
Jan 23, 2012
91
92
93
continue;
}
Nov 5, 2017
Nov 5, 2017
94
if ( SDL_strcmp(argv[i], "-save") == 0 && argv[i+1] ) {
Jun 3, 2013
Jun 3, 2013
95
96
97
98
99
++i;
saveFile = argv[i];
continue;
}
Jan 23, 2012
Jan 23, 2012
100
101
102
/* Open the image file */
texture = IMG_LoadTexture(renderer, argv[i]);
if (!texture) {
Oct 26, 2018
Oct 26, 2018
103
SDL_Log("Couldn't load %s: %s\n", argv[i], SDL_GetError());
Jan 23, 2012
Jan 23, 2012
104
105
106
107
continue;
}
SDL_QueryTexture(texture, NULL, NULL, &w, &h);
Jun 3, 2013
Jun 3, 2013
108
109
110
111
/* Save the image file, if desired */
if ( saveFile ) {
SDL_Surface *surface = IMG_Load(argv[i]);
if (surface) {
Nov 5, 2017
Nov 5, 2017
112
113
114
115
116
117
118
119
120
121
int result;
const char *ext = SDL_strrchr(saveFile, '.');
if ( ext && SDL_strcasecmp(ext, ".bmp") == 0 ) {
result = SDL_SaveBMP(surface, saveFile);
} else if ( ext && SDL_strcasecmp(ext, ".jpg") == 0 ) {
result = IMG_SaveJPG(surface, saveFile, 90);
} else {
result = IMG_SavePNG(surface, saveFile);
}
if ( result < 0 ) {
Oct 26, 2018
Oct 26, 2018
122
SDL_Log("Couldn't save %s: %s\n", saveFile, SDL_GetError());
Jun 3, 2013
Jun 3, 2013
123
124
}
} else {
Oct 26, 2018
Oct 26, 2018
125
SDL_Log("Couldn't load %s: %s\n", argv[i], SDL_GetError());
Jun 3, 2013
Jun 3, 2013
126
127
128
}
}
Jan 23, 2012
Jan 23, 2012
129
130
131
132
133
134
135
/* Show the window */
SDL_SetWindowTitle(window, argv[i]);
SDL_SetWindowSize(window, w, h);
SDL_ShowWindow(window);
done = 0;
while ( ! done ) {
Jun 8, 2013
Jun 8, 2013
136
while ( SDL_PollEvent(&event) ) {
Jan 23, 2012
Jan 23, 2012
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
switch (event.type) {
case SDL_KEYUP:
switch (event.key.keysym.sym) {
case SDLK_LEFT:
if ( i > 1 ) {
i -= 2;
done = 1;
}
break;
case SDLK_RIGHT:
if ( argv[i+1] ) {
done = 1;
}
break;
case SDLK_ESCAPE:
case SDLK_q:
argv[i+1] = NULL;
/* Drop through to done */
case SDLK_SPACE:
case SDLK_TAB:
done = 1;
break;
default:
break;
}
break;
case SDL_MOUSEBUTTONDOWN:
done = 1;
break;
case SDL_QUIT:
argv[i+1] = NULL;
done = 1;
break;
default:
break;
}
}
Jun 8, 2013
Jun 8, 2013
174
175
176
177
178
179
180
181
/* Draw a background pattern in case the image has transparency */
draw_background(renderer, w, h);
/* Display the image */
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
SDL_Delay(100);
Jan 23, 2012
Jan 23, 2012
182
183
184
185
}
SDL_DestroyTexture(texture);
}
Sep 12, 2017
Sep 12, 2017
186
187
188
189
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
Jan 23, 2012
Jan 23, 2012
190
191
192
/* We're done! */
SDL_Quit();
return(0);
Aug 10, 2000
Aug 10, 2000
193
}